I have 2 Arraylists. One contain title of song and another contain location of song. How can i sort them together?
Asked
Active
Viewed 95 times
3 Answers
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
-
I want to access the arraylist with index how can i do that in it – Shyam Sangeeth Apr 08 '19 at 13:31
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.

Shyam Sangeeth
- 49
- 6