Java 如何按字母顺序对我的 Arraylist 进行排序?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19757578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I sort my Arraylist alphabetical? (Java)
提问by vinzzenzz
I am kind of new in java and I have not already a big knowledge about coding but maybe you can help me with my problem. I have a program which should list all the songs in a alphabetical right order. I want to work with the Collections API. But in my code I don't get it right. Eclipse doesnt give me an error and on the emulator it runs, without to sort it right. Maybe you can show me, transferred to my code what I have to do, to get it running. Sorry for being not as good as you ;) ...But I am young and I want to learn all about coding. thanks a lot, Vinzenz :)
我是 Java 新手,我对编码还不是很了解,但也许您可以帮助我解决我的问题。我有一个程序应该按正确的字母顺序列出所有歌曲。我想使用 Collections API。但在我的代码中,我不明白。Eclipse 不会给我一个错误,并且在它运行的模拟器上没有正确排序。也许你可以向我展示,转移到我的代码中我必须做什么,让它运行。抱歉没有你那么好;) ...但我还年轻,我想学习所有关于编码的知识。非常感谢,文岑兹 :)
public class SongsManager {
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
private String mp4Pattern = ".mp4";
private String MP3Pattern = ".MP3";
private String MP4Pattern = ".MP4";
private String m4aPattern = ".m4a";
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern) || song.getName().endsWith(mp4Pattern) || song.getName().endsWith(MP4Pattern) || song.getName().endsWith(MP3Pattern) || song.getName().endsWith(m4aPattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
public static void main (String[] args) {
List<String> songsList = new LinkedList <String>();
System.out.println(songsList);
Collections.sort(songsList);
System.out.println(songsList);
}
}
采纳答案by Vidya
Just use Collections.sort(songsList)
--once you've populated the list as @Matt points out.
正如Collections.sort(songsList)
@Matt 指出的那样,只需使用--once 填充列表。
This will work because the items in songsList
are String
's, and String
implements Comparable
.
这将起作用,因为中的项目songsList
是String
's, 并且String
实现了Comparable
。