-1

I have 2 Arraylists. One contain title of song and another contain location of song. How can i sort them together?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

3 Answers3

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.

Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
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());
    }
});
karan
  • 8,637
  • 3
  • 41
  • 78
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]);
}

This fixed my problems.