简体   繁体   中英

how to sort nested lists in java

I have a json like below :

{ 
  "Student": [
    {  
      "name":"5",
      "Roll No":12345,
      "Subjects":[  
        {  
           "subjectCode":"Mat"
        },
        {  
           "subjectCode":"Sci"
        }
      ]
    } 
  ]
}

I want to sort list of subjects within each student and then sorting student objects with roll no.Can it be done with java8 in single line.

I am using below code :

list.forEach(studentObj -> {
    studentObj.getSubjects()
      .sort(Comparator.nullsLast(Comparator.comparing(Subject:: getSubjectCode)));
  });

then sorting the outside object

  list.sort(Comparator.nullsLast(Comparator.comparing(Student:: getRollNo)));

After parsing it to object you could write something like below:

List<Student> sorted = students.stream()
            .map(f -> new Student(f.getId(), f.getSubjects().stream().sorted(Comparator.comparing(Subject::getSubjectCode)).collect(Collectors.toList())))
            .sorted(Comparator.comparing(Student::getRollNo))
            .collect(Collectors.toList())

If you were to implement the Comparable interface with your 2 classes Student and Subject you could make it really nice and short because you can use the Comparator.naturalOrder() static factory method

Also it allows you to change the way you want to define how Subjects or Students get ordered by simply changing their compareTo method

Student

class Student implements Comparable<Student> {
    String name;
    int rollNo;
    List<Subject> subjects = new ArrayList<>();

    // GETTERS/SETTERS

    @Override
    public int compareTo(Student student) {
        return Integer.compare(this.getRollNo(), student.getRollNo());
    }

}

Subject

class Subject implements Comparable<Subject> {
    String subjectCode;

    // GETTERS/SETTERS

    @Override
    public int compareTo(Subject subject) {
        return this.getSubjectCode().compareTo(subject.getSubjectCode());
    }

}

And then

// static import to make code lighter
import static java.util.Comparator.*;

// sort Students list, then sort Subjects list in each Student
list.sort(nullsLast(naturalOrder()));
list.forEach(s -> { if (s != null) {
   s.getSubjects().sort(nullsLast(naturalOrder())); 
}});

One liner version if you really need it

List<Student> sortedList = list.stream()
    .map(s -> {
        if (s != null) {
            s.getSubjects().sort(nullsLast(naturalOrder()));
        }
        return s;
    }).sorted(nullsLast(naturalOrder()))
    .collect(Collectors.toList());

Here are a few one liner examples that should get you started, using java streams.

List<String> sorted = new ArrayList<>(Arrays.asList("Input1", "Input2", "Etc")).stream().sorted((a, b) -> a.compareTo(b));


List<String> sorted = json.stream().sorted((a, b) -> a.compareTo(b));


List<String> sorted = json.stream().sorted();

Here is how to sort all lists inside of a list, so you can refactor now to make it a one liner

void method() {
    sort(list);
}

void sort(List list) {
    if (list.size() == 0)
        return;

    list.stream().sorted();
    for (Object o : list)
        if (o instanceof List)
            sorted((List) o);
}

Here are some references to java streams:

API Docs:

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Examples:

https://stackify.com/streams-guide-java-8/

https://www.geeksforgeeks.org/stream-in-java/

Try this one

List<Student> result = new ArrayList<>();
studentList.stream()
           .sorted(Comparator.comparingInt(Student::getRollNo))
           .map(student -> {
                student.getSubjects().sort(Comparator.nullsLast(Comparator.comparing(Subjects::getSubjectCode)));
                return student;
            }).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM