Back to Java Notes
Topic #103

Java List Sorting


Java Sort a List

In the previous chapters, you learned how to use two popular lists in Java: ArrayList and LinkedList, which are found in the java.util package.

Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically.


Sort an ArrayList

Sort an ArrayList of Strings alphabetically in ascending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");

    Collections.sort(cars);  // Sort cars

    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Sort an ArrayList of Integers numerically in ascending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);

    Collections.sort(myNumbers);  // Sort myNumbers

    for (int i : myNumbers) {
      System.out.println(i);
    }
  }
}

Reverse the Order

You can also sort a list in reverse order, by using the reverseOrder() method.

In the following example, we sort an ArrayList of Strings alphabetically in reverse/descending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");

    Collections.sort(cars, Collections.reverseOrder()); // Sort cars

    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Sort an ArrayList of Integers numerically in reverse/descending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);

    Collections.sort(myNumbers, Collections.reverseOrder()); // Sort myNumbers

    for (int i : myNumbers) {
      System.out.println(i);
    }
  }
}

Want to go beyond the notes?

Join Coding Now Tech Institute's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java List Sorting – FAQs

Quick answers about learning Java List Sorting in Java.

This free note from Coding Now Tech Institute explains Java List Sorting in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on Coding Now Tech Institute, including Java List Sorting, is 100% free with no signup required.
With focused practice, most students grasp Java List Sorting in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now