Sort List of Integers in Ascending and Descending Order Example

In this example, we will use Collections.sort() method to sort a list of integers in ascending and descending order in Java.

Sort List of integers using Collections.sort() method

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortList {

    public static void main(String[] args) {

        // create list
        List < Integer > list = new ArrayList < Integer > ();
        list.add(10);
        list.add(30);
        list.add(20);
        list.add(50);
        list.add(40);

        Collections.sort(list); // ascending order 
        System.out.println(list);

        Collections.reverse(list); // descending order 
        System.out.println(list);
    }
}
Output:
[10, 20, 30, 40, 50]
[50, 40, 30, 20, 10]

References

  1. Sort List of Integers in Ascending and Descending Order Example
  2. List (ArrayList) Iterator Example
  3. Create the Immutable ArrayList with List.of() Method Example
  4. Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
  5. Java 10 - Copy List into Another List Exmaple
  6. Java 8 - Copy List into Another List Example
  7. Java - Copy a List to Another List using Collections.copy() method
  8. Java - Copy a List to Another List Example
  9. Java ArrayList spliterator() Method Example
  10. Java ArrayList sort() Method Example
  11. Java ArrayList retainAll() Method Example
  12. Java ArrayList removeIf() Method Example
  13. Java ArrayList removeAll() Method Example
  14. Java ArrayList remove() Method Example
  15. Java ArrayList lastIndexOf() Method Example
  16. Java ArrayList isEmpty() Method Example
  17. Java util ArrayList indexOf() Method Example
  18. Java ArrayList get() Method Example
  19. Java ArrayList ensureCapacity() Method Example
  20. Java ArrayList contains() Method Example
  21. Java ArrayList clone() Method Example
  22. Java ArrayList clear() Method Example
  23. Java ArrayList addAll() Method Example
  24. Java ArrayList add() Method Example
  25. Java 8 forEach() List Example
  26. Add Enum Values to ArrayList Example
  27. Join List Strings with Commas in Java
  28. Java Stream filter null values example
  29. Java ArrayList subList() Example
  30. Get Index of Elements in ArrayList Example
  31. Java ArrayList removeIf() Example
  32. Java ArrayList add(), get() and set() Method Example
  33. Iterate over ArrayList Using forEach Java
  34. Iterate over ArrayList using Iterator in Java
  35. Java ArrayList indexOf() and lastIndexOf() Example
  36. Search an Element in an ArrayList in Java
  37. Clear ArrayList in Java Example
  38. Java ArrayList removeAll() Method Example
  39. Java ArrayList remove() Method Example
  40. How to Iterate over ArrayList in Java
  41. How to Remove Element from ArrayList in Java
  42. How to Access Elements of ArrayList in Java
  43. Create ArrayList from Another ArrayList in Java
  44. How to Create an ArrayList and Add Elements to It

Comments