如何对2个依赖的ArrayList进行排序?

时间:2019-04-08 12:31:13

标签: java android

我有2个Arraylist。一个包含歌曲标题,另一个包含歌曲位置。如何将它们排序在一起?

3 个答案:

答案 0 :(得分:0)

Make a Class Like

public class Song{
  private String title;
  private String location;
}

Make a List<Song> and copy all the title and corresponding location of song in same object and save them to new List. and now Sort the new List.

答案 1 :(得分:0)

Create custom class Song

class Song {
    private String songName, SongUrl;

    public String getSongName() {
        return songName;
    }

    public void setSongName(String songName) {
        this.songName = songName;
    }

    public String getSonglink() {
        return Songlink;
    }

    public void setSonglink(String songlink) {
        Songlink = songlink;
    }
}

Instead of using two ArrayLists, Create single Arraylist with this class and use it as data source.

List<Song> songData = new ArrayList<Song>();

Now you can easily sort this using Comparator

Collections.sort(songData, new Comparator<Song>() {
    @Override
    public int compare(Song o1, Song o2) {
        return o1.getSongName().compareTo(o2.getSongName());
    }
});

答案 2 :(得分:0)

//loop start
     //some code
     arrayList.add(currenttitle+"////.////"+currentartist+" | "+currentalbum+"////.////"+currentlocation);
     //some code
//loop end
Collections.sort(arrayList,String.CASE_INSENSITIVE_ORDER);
for(int i=0;i<arrayList.size();i++) {
    arrayListloc.add(arrayList.get(i).split("////.////")[2]);
}

这解决了我的问题。