Skip to content

Commit d971cdd

Browse files
committed
feat(ComparatorDemo4): add demo of custom Comparator reversing integer order
What - Added custom comparator class My: - Implements Comparator<Integer>. - Overrides compare(Integer i1, Integer i2). - Returns positive if i1 < i2, negative if i1 > i2, 0 if equal. - Effectively reverses natural ordering (descending sort). - Added ComparatorDemo4 class: - Defines Integer array [2,4,6,8,1,3,5,7]. - Calls Arrays.sort(a, new My()) to sort using custom comparator. - Prints sorted array. Why - Demonstrates how to override default natural ordering (ascending) by supplying custom comparator. - Shows usage of Arrays.sort with a comparator. - Educational example contrasting Comparable (default) vs Comparator (custom). How - Arrays.sort(a, new My()): - Delegates element comparison to My.compare(). - Since My reverses the usual order, result is descending sort. - Iterated through sorted array and printed elements. Logic - Input array: [2,4,6,8,1,3,5,7]. - My.compare(i1,i2): - If i1 < i2 → return 1 → i1 placed after i2. - If i1 > i2 → return -1 → i1 placed before i2. - Output: [8,7,6,5,4,3,2,1]. - Edge cases: - Equal integers → comparator returns 0 → order remains stable. - Comparator must define total order; here logic is consistent with descending natural order. - Complexity / performance: O(n log n) sorting. - Concurrency / thread-safety: Not thread-safe; demo is safe with local array. - Error handling: None needed. Real-life applications - Sorting numbers in descending order (leaderboards, scores, rankings). - Custom ordering when natural order is not desired. - Comparator is flexible: can sort by arbitrary criteria (length, priority, timestamps). Notes - Without passing new My(), Arrays.sort() uses natural ordering (Comparable). - Comparator.comparingInt(i -> i).reversed() is a cleaner functional alternative. - Demonstrates old-style comparator implementation via class; modern style often uses lambdas. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8e5df9c commit d971cdd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
/**
4+
* This class demonstrates the usage of Arrays and the Comparator interface in Java.
5+
*
6+
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html">Comparator Interface </a>
7+
*/
8+
9+
class My implements Comparator<Integer> {
10+
//Implementing the compare method inside the comparator interface by reversing the order.
11+
public int compare(Integer i1,Integer i2) {
12+
if(i1<i2)
13+
return 1;
14+
if(i1>i2)
15+
return -1;
16+
return 0;
17+
}
18+
}
19+
20+
public class ComparatorDemo4 {
21+
public static void main(String[] args) {
22+
Integer a[] = {2, 4, 6, 8, 1, 3, 5, 7}; // Passing comparator and name of comparator class.
23+
24+
Arrays.sort(a, new My());
25+
// Arrays.sort(a.new My());
26+
// you can use without My() then comparable interface will be used.
27+
28+
for (Integer x : a)
29+
System.out.print(" " + x);
30+
}
31+
}

0 commit comments

Comments
 (0)