1+ package com .amitph .java .collections ;
2+
3+ import com .google .common .collect .ComparisonChain ;
4+ import lombok .Getter ;
5+ import lombok .RequiredArgsConstructor ;
6+ import lombok .ToString ;
7+ import org .apache .commons .lang3 .builder .CompareToBuilder ;
8+
9+ import java .util .ArrayList ;
10+ import java .util .Comparator ;
11+ import java .util .List ;
12+
13+ @ RequiredArgsConstructor
14+ public class MultiFieldSorter {
15+ private final List <Student > collection = new ArrayList <>(List .of (
16+ new Student (1L , "Ray" , 18 ),
17+ new Student (2L , "Bee" , 18 ),
18+ new Student (3L , "Ray" , 17 ),
19+ new Student (4L , "Bia" , 15 ),
20+ new Student (5L , "Ria" , 15 )
21+ ));
22+
23+ public void sortUsingComparator_Field_by_Field () {
24+ collection .sort (new SimpleComparator ());
25+ collection .forEach (System .out ::println );
26+ }
27+
28+ public void sortUsingComparator_Guava_ComparisonChain () {
29+ collection .sort (new GuavaBasedComparator ());
30+ collection .forEach (System .out ::println );
31+ }
32+
33+ public void sortUsingComparator_Commons_CompareToBuilder () {
34+ collection .sort (new ApacheCommonsBasedComparator ());
35+ collection .forEach (System .out ::println );
36+ }
37+
38+ public void sortUsingComparator_Comparing () {
39+ collection .sort (Comparator
40+ .comparing (Student ::getName )
41+ .thenComparing (Student ::getAge ));
42+ collection .forEach (System .out ::println );
43+ }
44+
45+ public static void main (String [] a ) {
46+ MultiFieldSorter sorter = new MultiFieldSorter ();
47+
48+ System .out .println ("Custom Comparator - Simple" );
49+ sorter .sortUsingComparator_Field_by_Field ();
50+
51+ System .out .println ();
52+ System .out .println ("Custom Comparator - Guava ComparisonChain" );
53+ sorter .sortUsingComparator_Guava_ComparisonChain ();
54+
55+ System .out .println ();
56+ System .out .println ("Custom Comparator - Commons CompareToBuilder" );
57+ sorter .sortUsingComparator_Commons_CompareToBuilder ();
58+
59+ System .out .println ();
60+ System .out .println ("Comparator Factory - Comparator.comparing()" );
61+ sorter .sortUsingComparator_Comparing ();
62+ }
63+ }
64+
65+ @ ToString
66+ @ Getter
67+ @ RequiredArgsConstructor
68+ class Student {
69+ private final long id ;
70+ private final String name ;
71+ private final int age ;
72+ }
73+
74+ class SimpleComparator implements Comparator <Student > {
75+ @ Override
76+ public int compare (Student o1 , Student o2 ) {
77+ int difference = o1 .getName ().compareTo (o2 .getName ());
78+
79+ return (difference != 0 ) ? difference
80+ : Integer .compare (o1 .getAge (), o2 .getAge ());
81+ }
82+ }
83+
84+ class GuavaBasedComparator implements Comparator <Student > {
85+ @ Override
86+ public int compare (Student o1 , Student o2 ) {
87+ return ComparisonChain .start ()
88+ .compare (o1 .getName (), o2 .getName ())
89+ .compare (o1 .getAge (), o2 .getAge ())
90+ .result ();
91+ }
92+ }
93+
94+ class ApacheCommonsBasedComparator implements Comparator <Student > {
95+ @ Override
96+ public int compare (Student o1 , Student o2 ) {
97+ return new CompareToBuilder ()
98+ .append (o1 .getName (), o2 .getName ())
99+ .append (o1 .getAge (), o2 .getAge ())
100+ .build ();
101+ }
102+ }
0 commit comments