ArrayList is one of the widely used data structure in java. In
ArrayList, elements are placed as they are inserted. But while coding,
you often need them in some order. In this post, we will see how to sort
an ArrayList with some simple examples.
To sort an ArrayList, we use sort() method of Collections class. Collections class is an utility class in java.util package consisting of many useful methods. (Collections and Collection are two different entities. Check this for more info). Collections.sort() method has two overloaded forms. They are,
Output :
ArrayList Before Sorting :
[Virat, rohit, Shikar, ashwin, ravindra, Bhargav]
ArrayList After Sorting :
[Bhargav, Shikar, Virat, ashwin, ravindra, rohit]
Output :
ArrayList Before Sorting :
[Virat, rohit, Shikar, ashwin, ravindra, Bhargav]
ArrayList After Sorting :
[ashwin, Bhargav, ravindra, rohit, Shikar, Virat]
Output :
listOfStudents Before Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 478, Name : Student4, Percentage : 94}, {ID : 365, Name : Student5, Percentage : 62}]
listOfStudents After Sorting :
[{ID : 85, Name : Student3, Percentage : 79}, {ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 365, Name : Student5, Percentage : 62}, {ID : 478, Name : Student4, Percentage : 94}]
Output :
listOfStudents Before Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 478, Name : Student4, Percentage : 94}, {ID : 365, Name : Student5, Percentage : 62}]
listOfStudents After Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 365, Name : Student5, Percentage : 62}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 478, Name : Student4, Percentage : 94}]
Output :
ArrayList Before Sorting :
[1452, 6854, 8741, 6542, 3845]
ArrayList Sorted In The Reverse Order :
[8741, 6854, 6542, 3845, 1452]
To sort an ArrayList, we use sort() method of Collections class. Collections class is an utility class in java.util package consisting of many useful methods. (Collections and Collection are two different entities. Check this for more info). Collections.sort() method has two overloaded forms. They are,
1) sort(List<T> list) : This method sorts the specified list according to natural ordering of its elements.
2) sort(List<T> list, Comparator<? super T> c) : This method sorts the specified list according to supplied Comparator.
These two methods are used not only just to sort the ArrayList, but
also other list types like LinkedList and Vector. Let’s see how to sort
an ArrayList with some simple examples.How To Sort An ArrayList Of Strings?
In this example, we are sorting an ArrayList of strings using first form of Collections.sort() method. This example sorts the string elements while considering the case of the elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| import java.util.ArrayList; import java.util.Collections; public class ListSorting { public static void main(String[] args) { //Creating an ArrayList of strings ArrayList<String> list = new ArrayList<String>(); //Adding elements to list list.add( "Virat" ); list.add( "rohit" ); list.add( "Shikar" ); list.add( "ashwin" ); list.add( "ravindra" ); list.add( "Bhargav" ); System.out.println( "ArrayList Before Sorting :" ); System.out.println(list); //Sorting the list Collections.sort(list); System.out.println( "ArrayList After Sorting :" ); System.out.println(list); } } |
ArrayList Before Sorting :
[Virat, rohit, Shikar, ashwin, ravindra, Bhargav]
ArrayList After Sorting :
[Bhargav, Shikar, Virat, ashwin, ravindra, rohit]
How To Sort An ArrayList Of Strings While Ignoring The Case?
To sort an ArrayList of strings while ignoring the case of the elements, we use second form of the Collections.sort() method which takes two arguments. One is the list to be sorted and another one is the Comparator. We pass String.CASE_INSENSITIVE_ORDER as Comparator here. This Comparator ignores the case of the string elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| import java.util.ArrayList; import java.util.Collections; public class ListSorting { public static void main(String[] args) { //Creating an ArrayList of strings ArrayList<String> list = new ArrayList<String>(); //Adding elements to list list.add( "Virat" ); list.add( "rohit" ); list.add( "Shikar" ); list.add( "ashwin" ); list.add( "ravindra" ); list.add( "Bhargav" ); System.out.println( "ArrayList Before Sorting :" ); System.out.println(list); //Sorting the list by ignoring the case Collections.sort(list, String.CASE_INSENSITIVE_ORDER); System.out.println( "ArrayList After Sorting :" ); System.out.println(list); } } |
ArrayList Before Sorting :
[Virat, rohit, Shikar, ashwin, ravindra, Bhargav]
ArrayList After Sorting :
[ashwin, Bhargav, ravindra, rohit, Shikar, Virat]
How To Sort An ArrayList Of Custom Objects?
In this example, we sort an ArrayList of Student objects. To do this, Student class must implement Comparable interface and override compareTo() method like below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| import java.util.ArrayList; import java.util.Collections; //Student class implementing Comparable interface class Student implements Comparable<Student> { int id; String name; int percentage; public Student( int id, String name, int percentage) { this .id = id; this .name = name; this .percentage = percentage; } @Override public int compareTo(Student s) { return this .id - s.id; //Sorts the objects in ascending order // return s.id - this.id; //Sorts the objects in descending order } @Override public String toString() { return "{ID : " +id+ ", Name : " +name+ ", Percentage : " +percentage+ "}" ; } } public class MainClass { public static void main(String[] args) { //Creating an ArrayList of Student objects ArrayList<Student> listOfStudents = new ArrayList<Student>(); //Adding students to listOfStudents listOfStudents.add( new Student( 123 , "Student1" , 62 )); listOfStudents.add( new Student( 231 , "Student2" , 81 )); listOfStudents.add( new Student( 85 , "Student3" , 79 )); listOfStudents.add( new Student( 478 , "Student4" , 94 )); listOfStudents.add( new Student( 365 , "Student5" , 62 )); System.out.println( "listOfStudents Before Sorting :" ); System.out.println(listOfStudents); //Sorting the listOfStudents Collections.sort(listOfStudents); System.out.println( "listOfStudents After Sorting :" ); System.out.println(listOfStudents); } } |
listOfStudents Before Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 478, Name : Student4, Percentage : 94}, {ID : 365, Name : Student5, Percentage : 62}]
listOfStudents After Sorting :
[{ID : 85, Name : Student3, Percentage : 79}, {ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 365, Name : Student5, Percentage : 62}, {ID : 478, Name : Student4, Percentage : 94}]
How To Sort An ArrayList Of Custom Objects Using Comparator?
In this example, we sort an ArrayList of Student objects by using our own Comparator. This Comparator sorts the Student objects based on their percentage.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; //Student class implementing Comparable interface class Student implements Comparable<Student> { int id; String name; int percentage; public Student( int id, String name, int percentage) { this .id = id; this .name = name; this .percentage = percentage; } @Override public int compareTo(Student s) { return this .id - s.id; //Sorts the objects in ascending order // return s.id - this.id; //Sorts the objects in descending order } @Override public String toString() { return "{ID : " +id+ ", Name : " +name+ ", Percentage : " +percentage+ "}" ; } } //Defining our own Comparator class OrderByPercentage implements Comparator<Student> { @Override public int compare(Student s1, Student s2) { return s1.percentage - s2.percentage; } } public class MainClass { public static void main(String[] args) { //Creating an ArrayList of Student objects ArrayList<Student> listOfStudents = new ArrayList<Student>(); //Adding students to listOfStudents listOfStudents.add( new Student( 123 , "Student1" , 62 )); listOfStudents.add( new Student( 231 , "Student2" , 81 )); listOfStudents.add( new Student( 85 , "Student3" , 79 )); listOfStudents.add( new Student( 478 , "Student4" , 94 )); listOfStudents.add( new Student( 365 , "Student5" , 62 )); System.out.println( "listOfStudents Before Sorting :" ); System.out.println(listOfStudents); //Sorting the listOfStudents by percentage Collections.sort(listOfStudents, new OrderByPercentage()); System.out.println( "listOfStudents After Sorting :" ); System.out.println(listOfStudents); } } |
listOfStudents Before Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 478, Name : Student4, Percentage : 94}, {ID : 365, Name : Student5, Percentage : 62}]
listOfStudents After Sorting :
[{ID : 123, Name : Student1, Percentage : 62}, {ID : 365, Name : Student5, Percentage : 62}, {ID : 85, Name : Student3, Percentage : 79}, {ID : 231, Name : Student2, Percentage : 81}, {ID : 478, Name : Student4, Percentage : 94}]
How To Sort An ArrayList In The Reverse Order?
You can sort the list in the reverse order also by passing the Comparator returned by Collections.reverseOrder() as Comparator to Collections.sort() method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| import java.util.ArrayList; import java.util.Collections; public class MainClass { public static void main(String[] args) { //Creating an ArrayList of integers ArrayList<Integer> list = new ArrayList<Integer>(); //Adding elements to list list.add( 1452 ); list.add( 6854 ); list.add( 8741 ); list.add( 6542 ); list.add( 3845 ); System.out.println( "ArrayList Before Sorting :" ); System.out.println(list); //Sorting the list in the reverse order Collections.sort(list, Collections.reverseOrder()); System.out.println( "ArrayList Sorted In The Reverse Order :" ); System.out.println(list); } } |
ArrayList Before Sorting :
[1452, 6854, 8741, 6542, 3845]
ArrayList Sorted In The Reverse Order :
[8741, 6854, 6542, 3845, 1452]
No comments:
Post a Comment