1

I have an ArrayList in one of my Java 15 project classes, which looks something like this:

ArrayList<String> myList = [Student1:15, Student2:38, Student3:87; Student4:10]

The ArrayList holds Strings where each element is the name of the student and their exam mark, with a colon in between.

The aim is to sort this list in order of lowest to highest mark. How would one go about doing that?

  • 2
    By writing a custom comparator that splits on the ":" ... see https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property for example. There are literally dozens, if not hundreds of examples on this site alone. – GhostCat Apr 22 '21 at 14:55
  • 1
    Unless there came up something new since Java 8: Use a [Comparator](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html) – gkhaos Apr 22 '21 at 14:56
  • 2
    And note: the "real world" answer: java is a statically typed language. So: use proper types. Meaning. Instead of working with raw strings **mangling** information using some "clever" formatting ... just create a Student class, and a class that models Marks. – GhostCat Apr 22 '21 at 14:57
  • 1
    And yes, that advice doesnt directly solve your problem. But rest assured: when you properly model your classes and create helpful abstractions, any kind of "client" code that you have to write to actually "do things" becomes way easier to write. – GhostCat Apr 22 '21 at 15:01
  • Someone suggested myList.sort(Comparator.comparing(o -> o.substring(o.indexOf(":")))); which works for any numbers < 10,000 What do I need to change so that 10,000 or over is correctly identified as bigger than 9999 – pepsiMaxFan Apr 22 '21 at 15:26
  • @Krombopulos you are using 10,000 with the comma as well? – dreamcrash Apr 22 '21 at 15:29
  • @dreamcrash I am not using with a comma. I have students with scores in their thousands (4 digits). They sort fine using the line I wrote in the previous comment. But some students have scores in their ten thousands, which are being treated like 4 digit scores by the comparator line of code for some reason For example, myList=[Student23:4049, Student45:2938, Student84:10397, Student 97:9123, Student99:21876] is sorted into [ Student84:10397, Student99:21876, Student45:2938, Student23:404, Student 97:9123] – pepsiMaxFan Apr 22 '21 at 15:35
  • @Krombopulos this answer https://stackoverflow.com/a/67215898/1366871 should solve that problem – dreamcrash Apr 22 '21 at 15:39
  • @dreamcrash It did. Thanks! – pepsiMaxFan Apr 22 '21 at 16:08

2 Answers2

1

You can do it like this:

    public static int grade(String s) {
        return Integer.parseInt(s.split(":")[1]);
    }

    public static void main(String[] args){
        List<String> l = new ArrayList<>();

        l.add("a:10");
        l.add("b:2");
        l.add("e:2");
        l.add("c:1");


        l.sort(Comparator.comparingInt(Main::grade));
        System.out.println(l);
    }
Jakub Dóka
  • 2,477
  • 1
  • 7
  • 12
0

Create an Object that implements Comparator and use Collections sort(List list, Comparator ) method.

Collections sort(List<T>,Comparator<? super T>) method example