Last active
August 29, 2015 14:25
-
-
Save MrSoftwareEngineer/2afbe9bb46863e4cd0c0 to your computer and use it in GitHub Desktop.
Create custom Comparator for sort items in the List by all fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
/* | |
*/ | |
public class CustomizedComparatorTest { | |
public static void main(String[] args) { | |
Comparator<MyPeople> comparatorAge = new Comparator<MyPeople>() { | |
@Override | |
public int compare(MyPeople myPeople, MyPeople t1) | |
{ | |
return myPeople.age - t1.age; | |
} | |
}; | |
Comparator<MyPeople> comparatorHeight = new Comparator<MyPeople>() { | |
@Override | |
public int compare(MyPeople myPeople, MyPeople t1) | |
{ | |
return myPeople.height - t1.height; | |
} | |
}; | |
Comparator<MyPeople> comparatorWeight = new Comparator<MyPeople>() { | |
@Override | |
public int compare(MyPeople myPeople, MyPeople t1) | |
{ | |
return myPeople.weight - t1.weight; | |
} | |
}; | |
Comparator<MyPeople> comparatorName = new Comparator<MyPeople>() { | |
@Override | |
public int compare(MyPeople myPeople, MyPeople t1) | |
{ | |
return myPeople.name.compareTo(t1.name); | |
} | |
}; | |
CustomizedComparator<MyPeople> customizedComparator = new CustomizedComparator<MyPeople>(comparatorAge, comparatorHeight, comparatorWeight, comparatorName); | |
ArrayList<MyPeople> peoples = new ArrayList<MyPeople>(); | |
peoples.add(new MyPeople("Ваня", 35, 180, 80)); | |
peoples.add(new MyPeople("Илья", 28, 165, 62)); | |
peoples.add(new MyPeople("Саша", 36, 185, 75)); | |
peoples.add(new MyPeople("Леша", 35, 175, 77)); | |
peoples.add(new MyPeople("Киря", 37, 172, 82)); | |
peoples.add(new MyPeople("Оля", 26, 165, 49)); | |
peoples.add(new MyPeople("Катя", 26, 165, 49)); | |
peoples.add(new MyPeople("Витя", 35, 185, 125)); | |
peoples.add(new MyPeople("Дима", 35, 180, 79)); | |
Collections.sort(peoples, customizedComparator); | |
for (MyPeople entry : peoples) { | |
System.out.println(entry); | |
} | |
} | |
public static class MyPeople { | |
private String name; | |
private int age; | |
private int height; | |
private int weight; | |
public MyPeople(String name, int age, int height, int weight) { | |
this.name = name; | |
this.age = age; | |
this.height = height; | |
this.weight = weight; | |
} | |
@Override | |
public String toString() { | |
return name + " " + age + " " + height + " " + weight; | |
} | |
} | |
public static class CustomizedComparator<T> implements Comparator<T> { | |
Comparator[] comparators; | |
public CustomizedComparator(Comparator... comparators) { | |
this.comparators = comparators; | |
} | |
@Override | |
public int compare(T t, T t1) { | |
for (int i = 0; i < comparators.length; i++) { | |
int compare = comparators[i].compare(t, t1); | |
if (compare != 0) { | |
return compare; | |
} | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment