WikiGalaxy

Personalize

Sorting Java List using Collections.sort()

Using Collections.sort() with Natural Order:

The Collections.sort() method sorts the list in the natural order. This is the simplest way to sort a list of comparable elements.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add("Banana");
      list.add("Apple");
      list.add("Cherry");
      Collections.sort(list);
      System.out.println(list);
  }
}
    

Console Output:

[Apple, Banana, Cherry]

Sorting Java List using Comparator

Using Comparator for Custom Order:

A Comparator can be used for custom sorting logic. For example, sorting strings by length.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add("Banana");
      list.add("Apple");
      list.add("Cherry");
      Collections.sort(list, new Comparator() {
          public int compare(String s1, String s2) {
              return s1.length() - s2.length();
          }
      });
      System.out.println(list);
  }
}
    

Console Output:

[Apple, Banana, Cherry]

Sorting Java List of Integers

Sorting Integers in Ascending Order:

Using Collections.sort() to sort integers in ascending order.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add(3);
      list.add(1);
      list.add(2);
      Collections.sort(list);
      System.out.println(list);
  }
}
    

Console Output:

[1, 2, 3]

Sorting Java List using Lambda Expression

Using Lambda for Custom Sorting:

Lambda expressions can simplify the syntax when implementing a Comparator for custom sorting.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add("Banana");
      list.add("Apple");
      list.add("Cherry");
      Collections.sort(list, (s1, s2) -> s2.compareTo(s1));
      System.out.println(list);
  }
}
    

Console Output:

[Cherry, Banana, Apple]

Sorting Java List of Custom Objects

Sorting Custom Objects by Property:

Use Comparator to sort a list of custom objects by a specific property.


import java.util.*;
class Person {
    String name;
    int age;
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return name + ":" + age;
    }
}
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add(new Person("Alice", 30));
      list.add(new Person("Bob", 25));
      list.add(new Person("Charlie", 35));
      Collections.sort(list, (p1, p2) -> p1.age - p2.age);
      System.out.println(list);
  }
}
    

Console Output:

[Bob:25, Alice:30, Charlie:35]

Sorting Java List in Reverse Order

Using Collections.reverseOrder():

The Collections.reverseOrder() method can be used to sort a list in reverse order.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add(3);
      list.add(1);
      list.add(2);
      Collections.sort(list, Collections.reverseOrder());
      System.out.println(list);
  }
}
    

Console Output:

[3, 2, 1]

Sorting Java List with Stream API

Using Stream.sorted():

Java 8 Stream API provides a sorted() method to sort a list in a functional style.


import java.util.*;
import java.util.stream.*;
class Example {
  public static void main(String args[]) {
      List list = Arrays.asList("Banana", "Apple", "Cherry");
      List sortedList = list.stream().sorted().collect(Collectors.toList());
      System.out.println(sortedList);
  }
}
    

Console Output:

[Apple, Banana, Cherry]

Sorting Java List with Multiple Criteria

Sorting by Multiple Criteria:

Use Comparator.thenComparing() to sort by multiple criteria, such as sorting by name and then by age.


import java.util.*;
class Person {
    String name;
    int age;
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return name + ":" + age;
    }
}
class Example {
  public static void main(String args[]) {
      List list = new ArrayList();
      list.add(new Person("Alice", 30));
      list.add(new Person("Bob", 25));
      list.add(new Person("Alice", 25));
      Collections.sort(list, Comparator.comparing(Person::getName).thenComparing(Person::getAge));
      System.out.println(list);
  }
}
    

Console Output:

[Alice:25, Alice:30, Bob:25]

Sorting Java List with Natural Order and Nulls First

Sorting with Null Values:

Use Comparator.nullsFirst() to handle null values while sorting.


import java.util.*;
class Example {
  public static void main(String args[]) {
      List list = Arrays.asList("Banana", null, "Cherry", "Apple");
      Collections.sort(list, Comparator.nullsFirst(Comparator.naturalOrder()));
      System.out.println(list);
  }
}
    

Console Output:

[null, Apple, Banana, Cherry]

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025