Java ArrayList Sorting - 1

If you are new to java Array List please check my previous post on java array list - Java Array List - 1

#First create an Integer type Array List.
ArrayList<Integer> arrayList = new ArrayList<>();

Add integers to Array List.
arrayList.add(10);
arrayList.add(30);
arrayList.add(20);
arrayList.add(100);

Now print the arrayList using :
System.out.println(arrayList);

The output will be: [10, 30, 20, 100]

Now all you need to do sort the array List in ascending(small to greater) order is :
Collections.sort(arrayList); 
*import Collections using import java.util.Collections;
print the arrayList , the output will be : [10, 20, 30, 100]

#for descending sort use
Collections.sort(arrayList, Collections.reverseOrder()); 
print the arrayList , the output will be : [100, 30, 20, 10]

Complete code :

Check next tutorial on this series - Java ArrayList Sorting - 2

Comments

Popular Posts