How to sort Numbers ArrayList in java with examples

It is a short tutorial about Different ways to sort the list of numbers in java.

Let us say We have a set of numbers say 100,200,4,5,79,26.

We can use either Array or List implementation(ArrayList or LinkedList in java.util package used to sort numbers

Java8 How to sort the list of numbers with the stream.sorted()

It is an example for sort ArrayList of numbers

  • natural order
  • ascending order
  • descending order

Here is the sequence of steps

  • Create an ArrayList with inline initialization using Arrays.asList method
  • Iterate using stream method and return stream
  • sorted again sorted the stream in the natural order and returns stream
  • Finally, Returns collection using the collect method

For descending order, you can override the sorted method as follows.

  sorted((item1, item2) -> item2.compareTo(item1)

For ascending order, you can override the sorted method as follows.

  sorted((item1, item2) -> item1.compareTo(item2)

Here is an complete example

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(100,200,4,5,79,26);
        System.out.println("Initial List: "+list);

        List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList());
        System.out.println("Sorted Natural List: "+sortedList);
        List<Integer> sortedDescendingList = list.stream().sorted((item1, item2) -> item2.compareTo(item1)).collect(Collectors.toList());
        List<Integer> sortedAscendingList = list.stream().sorted((item1, item2) -> item1.compareTo(item2)).collect(Collectors.toList());

        System.out.println("Sorted Ascending List: "+sortedAscendingList);
        System.out.println("Sorted Descending List: "+sortedDescendingList);

    }
}

Initial List: [100, 200, 4, 5, 79, 26]
Sorted Natural List: [4, 5, 26, 79, 100, 200]
Sorted Ascending List: [4, 5, 26, 79, 100, 200]
Sorted Descending List: [200, 100, 79, 26, 5, 4]

Collections sort to sort numbers in natural and reverse order example

we have used the Sort method for natural order sorting

  • List.sort() method
  • Collections.sort() method

For reverse order, you can use reverse methods

  • List.reverse() method
  • Collections.reverse() method
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        Integer[] numbs = new Integer[]{100, 200, 4, 5, 79, 26};
        System.out.println("Initial List=" + numbs);
        /*For sorting numbers we can use Arrays's sort method*/
        Arrays.sort(numbs);

        Integer[] numbs1 = new Integer[]{100, 200, 4, 5, 79, 26, 20};

        /*For sorting numbers we can also use List's  sort method*/
        List listNumbers = Arrays.asList(numbs1);
        Collections.sort(listNumbers);
        System.out.println("Sort List using List sort=" + listNumbers);

        Arrays.sort(numbs);
        Collections.sort(listNumbers);
        System.out.println("List reverse order=" + numbs);
        System.out.println("Collections reverse order=" + listNumbers);


    }
}

Arrays can store the static list of data items, which means the size cannot be grown and fixed.

whereas List store the list of dynamic data times.

Conclusion

We have learned different ways to sort numbers in a list with java8 streams and the sort method.