Answer the question
In order to leave comments, you need to log in
Sort ArrayList java?
There is this code:
class Type
{
float length;
String name;
public int compareTo(Type tmp) {
return (int) (this.length - tmp.length);
}
}
...
public ArrayList<Type> music;
music = new ArrayList<Type>();
...
Arrays.sort(music);
public void LoadFromFiles()
{
try {
File file = new File("C:\\Users\\User\\source\\repos\\Project1\\Project1\\playlist4.m3u8");
//создаем объект FileReader для объекта File
FileReader fr = new FileReader(file);
//создаем BufferedReader с существующего FileReader для построчного считывания
BufferedReader reader = new BufferedReader(fr);
// считаем сначала первую строку
Type tmp = new Type();
tmp.length = (float) 0.0;
tmp.name = "";
String line = reader.readLine();
while (line != null) {
tmp.name = line;
line = reader.readLine();
tmp.length = Float.parseFloat(line);
music.add(tmp);
//System.out.println(tmp.name+"\n"+tmp.length);
// считываем остальные строки в цикле
line = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
float length = (float) 3600.0, currentlength = (float) 0.0;
Collections.sort(music);
for(Type t:music)
{
System.out.println(t.name+"\n"+t.length);
}
}
Answer the question
In order to leave comments, you need to log in
Arrays is for arrays.
For List, use Collections.Collections.sort(music):
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question