Java Tutorial series - Collections - Sorting Lists
By this time, we already know how to perform sorting operations in HashMap and Sets. Now, let's have a look at Sorting operations for LinkedList and ArrayList. It can be performed with the help of the Comparator.
Sorting ArrayList in Alphabetical order -
public class App {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("Mango");
fruits.add("Watermelon");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Banana");
Collections.sort(fruits);
for(String fruit: fruits) {
System.out.println(fruit);
}
}
}
The output of the above program -
}
}
The output of the above program -
Apple
Banana
Mango
Orange
Watermelon
Sorting ArrayList based on String Length using Comparator -
We will be taking similar example given above, after declaring the implementation, we have to add unimplemented methods from Comparator -
Class StringLengthComparator implements Comparator<String> {
@Override
public int compare(String S1, String S2)
{
int len1 = S1.length();
int len2 = S2.length();
if(len1 > len2) {
return 1;
}
else if(len1 < len2) {
return -1;
}
return 0;
}
public class App {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("Mango");
fruits.add("Watermelon");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Banana");
Collections.sort(fruits, new StringLengthComparator());
for(String fruit: fruits) {
System.out.println(fruit);
}
}
}
}
}
The output of the above program -
Apple
Mango
Banana
Orange
Watermelon
Sorting in alphabetical order using comparator -
class AlphabeticalComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
}
public class App {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("Mango");
fruits.add("Watermelon");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Banana");
Collections.sort(fruits, new AlphabeticalComparator());
for(String fruit: fruits) {
System.out.println(fruit);
}
}
}
}
}
Output -
Apple
Banana
Mango
Orange
Watermelon
Sorting in reverse alphabetical order using comparator -
class ReverseAlphabeticalComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return -s1.compareTo(s2);
}
}
public class App {
public static void main(String[] args) {
List<String> fruits = new ArrayList<String>();
fruits.add("Mango");
fruits.add("Watermelon");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Banana");
Collections.sort(fruits, new ReverseAlphabeticalComparator());
for(String fruit: fruits) {
System.out.println(fruit);
}
}
}
Output -
Watermelon
Orange
Mango
Banana
Apple
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
if(p1.getId() > p2.getId() {
reetun 1;
}
else if (p1.getId() < p2.getId() {
return -1;
}
rerurn 0;
});
for(Person person:people) {
System.out.println(person);
}
Above program was sorted based upon id, we can also sort based upon the name, only a small addition in code is needed -
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
});
for(Person person:people) {
System.out.println(person);
}
Sorting by Arbitrary objects (id) -
class Person {
private int id;
private string name;
public Person(int id, String name) {
this.id=id;
this.name = name;
//Add getter and setter methods by right click- > Source -> Generate getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public String toString() {
return id + " " + name ;
}
List<Person> people = new ArrayList<Person>();
people.add(new Person(1, "Joe");
people.add(new Person(3, "Bob");
people.add(new Person(4, "Clare");
people.add(new Person(2, "Sue");
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
if(p1.getId() > p2.getId() {
reetun 1;
}
else if (p1.getId() < p2.getId() {
return -1;
}
rerurn 0;
});
for(Person person:people) {
System.out.println(person);
}
Above program was sorted based upon id, we can also sort based upon the name, only a small addition in code is needed -
Sorting by Arbitrary objects (name) -
class Person {
private int id;
private string name;
public Person(int id, String name) {
this.id=id;
this.name = name;
//Add getter and setter methods by right click- > Source -> Generate getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public String toString() {
return id + " " + name ;
}
List<Person> people = new ArrayList<Person>();
people.add(new Person(1, "Joe");
people.add(new Person(3, "Bob");
people.add(new Person(4, "Clare");
people.add(new Person(2, "Sue");
Collections.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
});
for(Person person:people) {
System.out.println(person);
}
Comments
Post a Comment