0

I have a List:

 LinkedList<Student> student = new LinkedList<Student>();

In the Student class, it has an attribute String name. I want to sort the List students by its name. Is there any way I could do that?

DAIRAV
  • 723
  • 1
  • 9
  • 31
Ryan
  • 13
  • 1
  • 2

5 Answers5

3
student.sort(Comparator.comparing(Student::getName));

Read more about comparators.

Kartik
  • 7,677
  • 4
  • 28
  • 50
1

You can define a Comparator to do this. One easy way is with an anonymous class. https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

Collections.sort(student, new Comparator<Student>() {
    public int compare(Student a, Student b) {
        return a.getName().compare(b.getName());
    }
};
Oxy
  • 71
  • 6
0

You can use the Collections.sort method accepts a comparator as its second argument. Pass in a comparator that defines the ordering that you would like. For example given a Student class, you can use Collections.sort with a custom comparator to sort Student by ascending order of name like this:

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

class Student {

    private final String name;
    private final int score;

    Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", score=" + score + "]";
    }

    public static void main(String[] args) {
        List<Student> list = new LinkedList<>(
                Arrays.asList(new Student("David", 3), new Student("Alice", 3), new Student("Thomas", 9)));

        System.out.println("Before Sort: " + list);

        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("After Sort: " + list);
    }

}

This will produce the output

before: [Student [name=David, score=3], Student [name=Alice, score=3], Student [name=Thomas, score=9]]
after: [Student [name=Alice, score=3], Student [name=David, score=3], Student [name=Thomas, score=9]]
1218985
  • 7,531
  • 2
  • 25
  • 31
0

if you use java > 8. You can use Lambda expression, and you get something like this:

Collections.sort(student, (p1, p2) -> p1.name.compareTo(p2.name));

or Using sort method in List

student.sort((p1, p2) -> p1.name.compareTo(p2.name));

Pascal Fares
  • 21,959
  • 2
  • 14
  • 12
0

simply you can use this

   student.sort(Comparator.comparing(Student:: getName));//Single Sorting
    student.sort(Comparator.comparing(Student:: getName).reversed());//Single reverse Sorting
    student.sort(Comparator.comparing(Student:: getName).thenComparing(Student::getScore));//Double
Faiz Akram
  • 559
  • 4
  • 10