ArrayList
are also called as dynamic arrays that can grow as needed. In Java
ArrayList class extends AbstractList and implements List interface. In
Java ArrayList’s are created with an initial size. The size of the
collection is changes dynamically as the items are exceeded the size or
deleted. This example will explain sorting of ArrayList
using Comparator, Comparable and Collectiolns.sort() method.
Note:
Typically standard Array in java is fixed sized in nature. Once
created, they cannot grow or shrink, which means that you must know in
advance how many elements an array will hold.
At times, you may
need to sort the ArrayList to make it alphabetically order. In this
below example, it shows the use of Collections.sort and comparator to
sort an ArrayList.
1. Sorting using 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
| public class SortArrayList {
public static void main(String[] args) {
ArrayList unsortList = new ArrayList();
unsortList.add("1011");
unsortList.add("5460");
unsortList.add("1000");
unsortList.add("Abdul");
unsortList.add("1012");
unsortList.add("Adam");
System.out.println("********** Value before sorting **************");
for (String str : unsortList) {
System.out.println(str);
}
Collections.sort(unsortList);
System.out.println("********** Value after sorting **************");
for (String str : unsortList) {
System.out.println(str);
}
}
}
|
Here in this above example the sorting happens according to ASCII.
Output:
2. Sorting objects in an ArrayList using Comparable interface
In
my example I want to sort the employees list as per their salary in
descending order. My Employee class implements Comparable interface and
overridden compareTo method. In Compare to method add your logic,
weather to sort ascending or descending order.
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
| public class Employee implements Comparable {
private String name;
private String id;
private double salary;
public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public int compareTo(Employee emplyoee) {
double compareSalary = ((Employee) emplyoee).getSalary();
// ascending order
// return (int) (this.salary - compareSalary);
// descending order
return (int) (compareSalary - this.salary);
}
@Override
public String toString() {
return "[ id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
}
|
3. Sorting Objects in an ArrayList using Comparator
Check out the below code snippet, I am sorting Employees list using Anonymous Comparator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| //Sorting using Anonymous inner class type
Collections.sort(emplyoees, new Comparator() {
@Override
public int compare(Employee e1, Employee e2) {
String id1 = ((Employee) e1).getId();
String id2 = ((Employee) e2).getId();
// ascending order
return id1.compareTo(id2);
// descending order
//return id2.compareTo(id1);
}
});
|
Complete Code
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
| public class SortArrayListObjects {
public static void main(String[] args) {
ArrayList emplyoees = new ArrayList();
emplyoees.add(new Employee("1001", "Adam", 5000));
emplyoees.add(new Employee("1006", "Rajan", 8400));
emplyoees.add(new Employee("1040", "Keay", 2500));
emplyoees.add(new Employee("1056", "Ashok", 12000));
System.out.println("**** Value before sorting ***");
for(Employee emplyoee: emplyoees){
System.out.println(emplyoee);
}
Collections.sort(emplyoees);
System.out.println("**** After sorting salary decending ***");
for(Employee emplyoee: emplyoees){
System.out.println(emplyoee);
}
//Sorting using Anonymous inner class type
Collections.sort(emplyoees, new Comparator() {
@Override
public int compare(Employee e1, Employee e2) {
String id1 = ((Employee) e1).getId();
String id2 = ((Employee) e2).getId();
// ascending order
return id1.compareTo(id2);
// descending order
//return id2.compareTo(id1);
}
});
System.out.println("**** After sorting id accending ***");
for(Employee emplyoee: emplyoees){
System.out.println(emplyoee);
}
}
}
|
Output
Comments
Post a Comment