- Sort ArrayList
- Sort ArrayList in Descending order
- Sort ArrayList of Objects using Comparable and Comparator
Last active
July 16, 2018 19:07
-
-
Save amelieykw/830bcd9f26125c77b055e5460c3ea8fb to your computer and use it in GitHub Desktop.
[Java - ArrayList Sorting] #Java #ArrayList #Sorting #tutorial #interview
examples of sorting an String ArrayList and Integer ArrayList.
by simply calling the Collections.sort(arraylist)
method.
The output List will be sorted alphabetically.
import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<String> listofcountries = new ArrayList<String>();
listofcountries.add("India");
listofcountries.add("US");
listofcountries.add("China");
listofcountries.add("Denmark");
/*Unsorted List*/
System.out.println("Before Sorting:");
for(String counter: listofcountries){
System.out.println(counter);
}
/* Sort statement*/
Collections.sort(listofcountries);
/* Sorted List*/
System.out.println("After Sorting:");
for(String counter: listofcountries){
System.out.println(counter);
}
}
}
Output:
Before Sorting:
India
US
China
Denmark
After Sorting:
China
Denmark
India
US
The same Collections.sort()
method can be used for sorting the Integer ArrayList as well.
import java.util.*;
public class ArrayListOfInteger {
public static void main(String args[]){
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(11);
arraylist.add(2);
arraylist.add(7);
arraylist.add(3);
/* ArrayList before the sorting*/
System.out.println("Before Sorting:");
for(int counter: arraylist){
System.out.println(counter);
}
/* Sorting of arraylist using Collections.sort*/
Collections.sort(arraylist);
/* ArrayList after sorting*/
System.out.println("After Sorting:");
for(int counter: arraylist){
System.out.println(counter);
}
}
}
Output:
Before Sorting:
11
2
7
3
After Sorting:
2
3
7
11
We are using Collections.reverseOrder()
method along with Collections.sort()
in order to sort the list in decreasing order. In the below example we have used the following statement for sorting in reverse order.
Collections.sort(arraylist, Collections.reverseOrder());
However the reverse order sorting can also be done as following – This way the list will be sorted in ascending order first and then it will be reversed.
Collections.sort(list);
Collections.reverse(list);
import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add("AA");
arraylist.add("ZZ");
arraylist.add("CC");
arraylist.add("FF");
/*Unsorted List: ArrayList content before sorting*/
System.out.println("Before Sorting:");
for(String str: arraylist){
System.out.println(str);
}
/* Sorting in decreasing order*/
Collections.sort(arraylist, Collections.reverseOrder());
/* Sorted List in reverse order*/
System.out.println("ArrayList in descending order:");
for(String str: arraylist){
System.out.println(str);
}
}
}
Output:
Before Sorting:
AA
ZZ
CC
FF
ArrayList in descending order:
ZZ
FF
CC
AA
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment