E
E
Evgeny Petryaev2019-11-07 11:28:36
Java
Evgeny Petryaev, 2019-11-07 11:28:36

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);

And it gives errors not suitable method found for sort
PS I don’t understand why there is one value in all array indices:
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

1 answer(s)
D
Dmitry Roo, 2019-11-07
@Gremlin92

Arrays is for arrays.
For List, use Collections.
Collections.sort(music):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question