[英]How would i alphabetically sort this arraylist?
所以我有一个带有哈希表的ArrayList
,该哈希表返回歌曲列表。 所以我的问题是,我将如何修改此代码,使其按字母顺序返回歌曲?
继承人的代码...
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
您将需要实现一个Comparator
并使用Collections.sort
请参阅以下相关答案: https : //stackoverflow.com/a/18441978/288915
您可以将Collections.sort
与用于比较歌曲标题的anonymous comparator
一起使用,例如:
Collections.sort(list, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> o1, Map<String, String> o2) {
if (o1.containsKey("songTitle") && o2.containsKey("songTitle")) {
return o1.get("songTitle").compareTo(o2.get("songTitle"));
}
return 0;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.