In this article, we will discuss how to sort TreeSet elements in descending order using Comparator interface
1. Comparator interface
- Compares its two arguments for order
- Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second argument
- Method signature :- int compare(T object1, T object2)
1.1 Sorting TreeSet of Integer numbers
- A TreeSet contains integer numbers in natural order ( or ascending order)
- We are sorting these integer numbers in descending order by passing Collections.reverseOrder() comparator as constructor argument to new TreeSet object
- Note: line no. 40/41
SortTreeSetOfIntegerInDescUsingComparator.java
package net.bench.resources.sort.treeset.java;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class SortTreeSetOfIntegerInDescUsingComparator {
public static void main(String[] args) {
// 1. create TreeSet
Set<Integer> numbers = new TreeSet<>();
// 1.2 add integer numbers to TS
numbers.add(181);
numbers.add(219);
numbers.add(529);
numbers.add(761);
numbers.add(431);
numbers.add(671);
numbers.add(351);
// 1.3 original TreeSet in insertion order
System.out.println("1. Original TreeSet<Integer> in ASC order :- \n");
// 1.4 Iterating using enhanced for-loop
for(int num : numbers){
System.out.println(num);
}
// 2. Reverse Order
System.out.println("\n2. TreeSet<Integer> in DESC order :- \n");
// 2.1 reverse order
Set<Integer> reverseNumbers = new TreeSet<>(Collections.reverseOrder());
reverseNumbers.addAll(numbers);
// 2.2 Iterating using enhanced for-loop
for(int revNum : reverseNumbers){
System.out.println(revNum);
}
}
}
Output:
1. Original TreeSet<Integer> in ASC order :-
181
219
351
431
529
671
761
2. TreeSet<Integer> in DESC order :-
761
671
529
431
351
219
181
1.2 Sorting TreeSet of String elements
- A TreeSet contains String elements in alphabetical order ( or ascending order)
- We are sorting these String elements in reverse alphabetical order by passing Collections.reverseOrder() comparator as constructor argument to new TreeSet object
- Note: line no. 41/42
SortTreeSetOfStringInDescUsingComparator.java
package net.bench.resources.sort.treeset.java;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class SortTreeSetOfStringInDescUsingComparator {
public static void main(String[] args) {
// 1. create TreeSet
Set<String> names = new TreeSet<>();
// 1.1 add names to TS
names.add("Saranya");
names.add("Karthika");
names.add("Amudha");
names.add("Pushpa");
names.add("Bhagyalakshmi");
names.add("Meena");
// 1.2 original TreeSet in insertion order
System.out.println("1. Original TreeSet<String> "
+ "in alphabetical order :- \n");
// 1.3 Iterating using enhanced for-loop
for(String name : names){
System.out.println(name);
}
// 2. Sorting Reverse Alphabetical order
System.out.println("\n2. TreeSet<String> "
+ "in reverse alphabetical order :- \n");
// 2.1 reverse alphabetical order
Set<String> reverseNames = new TreeSet<>(Collections.reverseOrder());
reverseNames.addAll(names);
// 2.2 Iterating using enhanced for-loop
for(String revName : reverseNames){
System.out.println(revName);
}
}
}
Output:
1. Original TreeSet<String> in alphabetical order :-
Amudha
Bhagyalakshmi
Karthika
Meena
Pushpa
Saranya
2. TreeSet<String> in reverse alphabetical order :-
Saranya
Pushpa
Meena
Karthika
Bhagyalakshmi
Amudha
1.3 Sorting TreeSet of Employee objects
- A TreeSet contains Employee objects with attributes such as Id, name and their salary in ascending order of their Id‘s
- We are sorting these Employee objects in descending order by passing Collections.reverseOrder() comparator as constructor argument to new TreeSet object
- Note: line no. 41/42
Employee.java
package net.bench.resources.sort.treeset.java;
public class Employee implements Comparable<Employee> {
// member variables
private int id;
private String name;
private long salary;
// 3-arg parameterized constructor
// getters and setters
// toString()
// compareTo
@Override
public int compareTo(Employee emp) {
return Long.compare(this.getId(), emp.getId());
}
}
SortTreeSetOfEmployeesInDescUsingComparator.java
package net.bench.resources.sort.treeset.java;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class SortTreeSetOfEmployeesInDescUsingComparator {
public static void main(String[] args) {
// 1. create TreeSet
Set<Employee> employees = new TreeSet<>();
// 1.1 add Employees to TS
employees.add(new Employee(103, "Amudha", 49000L));
employees.add(new Employee(106, "Meena", 38000L));
employees.add(new Employee(105, "Bhagyalakshmi", 29000L));
employees.add(new Employee(102, "ThenMozhi", 57000L));
employees.add(new Employee(101, "Saranya", 85000L));
employees.add(new Employee(104, "Pushpa", 24000L));
// 1.2 original TreeSet in insertion order of Id
System.out.println("1. Original TreeSet "
+ "as per sorted-order of Id :- \n");
// 1.3 Iterating using enhanced for-loop
for(Employee emp : employees){
System.out.println(emp);
}
// 2. Sorting Employee - decreasing salary
System.out.println("\n2. Sorted Employee TreeSet - "
+ "according to decreasing Id :- \n");
// 2.1 sort acc. to decreasing-order of Id
Set<Employee> reverseEmployees = new TreeSet<>(Collections.reverseOrder());
reverseEmployees.addAll(employees);
// 2.2 Iterating using enhanced for-loop
for(Employee revEmp : reverseEmployees){
System.out.println(revEmp);
}
}
}
Output:
1. Original TreeSet as per sorted-order of Id :-
Employee [id=101, name=Saranya, salary=85000]
Employee [id=102, name=ThenMozhi, salary=57000]
Employee [id=103, name=Amudha, salary=49000]
Employee [id=104, name=Pushpa, salary=24000]
Employee [id=105, name=Bhagyalakshmi, salary=29000]
Employee [id=106, name=Meena, salary=38000]
2. Sorted Employee TreeSet - according to decreasing salary :-
Employee [id=106, name=Meena, salary=38000]
Employee [id=105, name=Bhagyalakshmi, salary=29000]
Employee [id=104, name=Pushpa, salary=24000]
Employee [id=103, name=Amudha, salary=49000]
Employee [id=102, name=ThenMozhi, salary=57000]
Employee [id=101, name=Saranya, salary=85000]
Related Articles:
- Sorting ArrayList using Comparable and Comparator
- Sorting ArrayList in descending order
- How to sort LinkedList using Collections.sort() method
- How to sort Vector using Collections.sort()
- Sorting list of objects on multiple fields using Comparator in Java
- Sorting HashSet contents in ascending and descending order
- How to Sort HashSet in Java – 2 ways
- How to sort HashSet in Java 8 ?
- How to sort LinkedHashSet contents in Java ?
- How to sort TreeSet in descending order using Comparator ?
- Sorting Collection of String, StringBuffer and StringBuilder in Java
References:
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Happy Coding !!
Happy Learning !!