How to create this java comparator
31,566
Solution 1
Test implements Comparator
and override compare()
method
public class Test implements Comparator<Test>{
private int priority;
private String desciption;
@Override
public int compare(Test o1, Test o2) {
// your code here
}
}
Solution 2
Comparator<Test> myComparator = new Comparator<Test>() {
public int compare(Test o1, Test o2) {
int result = o2.desciption.compareTo(o1.desciption);
if (result == 0) {
return o1.priority - o2.priority;
}
return result;
}
};
List<Test> sortedList = Collections.sort(testList, myComparator);
Author by
Fabio Mignogna
Updated on July 09, 2022Comments
-
Fabio Mignogna almost 2 years
I have this class:
public class Test { private int priority; private String desciption; ... }
and I have this arraylist:
Priority: 1, Description: C2 Priority: 2, Description: C1 Priority: 3, Description: C1 Priority: 4, Description: C2
I want this result:
Priority: 1, Description: C2 Priority: 4, Description: C2 Priority: 2, Description: C1 Priority: 3, Description: C1
How I have to do this with comparator? Thank you!
EDIT:
I reply to all of your questions:
@retro: no. description is a string without a specific format. it can be empty, too.
@tobias_k: first "group" by description and than order by priority. priority is always not equals.
@user270349:
this is my try:
int result = 0; if (o1.getPriority() < o2.getPriority()) result = -1; else if (o1.getPriority() > o2.getPriority()) result = 1 result *= o1.getDescription().compareTo(o2.getDescription()); return result;
EDIT 2:
another input/output example:
and I have this arraylist:
Priority: 1, Description: C2 Priority: 2, Description: C3 Priority: 3, Description: C1 Priority: 4, Description: C2
I want this result:
Priority: 1, Description: C2 Priority: 4, Description: C2 Priority: 2, Description: C3 Priority: 3, Description: C1
THIS IS MY SOLUTION:
List<Test> testList = new ArrayList<>(); testList.add(new Test(4, "C2")); testList.add(new Test(2, "C3")); testList.add(new Test(3, "C1")); testList.add(new Test(1, "C2")); Comparator<Test> comparator = new Comparator<Test>() { @Override public int compare(Test o1, Test o2) { int res = o1.getDescription().compareTo(o2.getDescription()); if (res == 0) return o1.getPriority() < o2.getPriority() ? -1 : 1; else return res; } }; Collections.sort(testList, comparator); List<String> valoriInseriti = new ArrayList<>(); List<Test> grouping = new ArrayList<>(); for (Test t : testList) { if (!valoriInseriti.contains(t.getDescription())) { valoriInseriti.add(t.getDescription()); grouping.add(t); } } comparator = new Comparator<Test>() { @Override public int compare(Test o1, Test o2) { return o1.getPriority() < o2.getPriority() ? -1 : 1; } }; Collections.sort(grouping, comparator); Collections.sort(testList, comparator); List<Test> output = new ArrayList<>(); for (Test t1 : grouping) { for (Test t2 : testList) { if (t2.getDescription().equals(t1.getDescription())) { output.add(t2); } } } System.out.println("=============================="); for (Test t : output) System.out.println(t);
-
tobias_k over 10 yearsShouldn't it rather implement
Comparable
? -
Fabio Mignogna over 10 yearsthis example work with example, but if i have this input Priority: 1, Description: C2 Priority: 2, Description: C3 Priority: 3, Description: C1 Priority: 4, Description: C2 the output should be Priority: 1, Description: C2 Priority: 4, Description: C2 Priority: 2, Description: C3 and your example doesn't work. thank you anyway!
-
Fabio Mignogna over 10 yearsthis example work with example, but if i have this input Priority: 1, Description: C2 Priority: 2, Description: C3 Priority: 3, Description: C1 Priority: 4, Description: C2 the output should be Priority: 1, Description: C2 Priority: 4, Description: C2 Priority: 2, Description: C3 and your example doesn't work. thank you anyway!
-
Prabhakaran Ramaswamy over 10 years@FabioMignogna Please update your question with your input and expected answer.
-
vickirk over 10 yearsShouldn't it rather implement Only if this comparison is the logical way to compare then, quite/most often the required order of comparison is required it in a different component and there may well be different sorting requirements.
-
tobias_k over 10 yearsAgreed that
Comparable
should only be used for the one natural comparison (if any), and any "funny" comparisons should be done withComparator
. But then IMHO theComparator
should be a separate class, and not the class of the object to be compared. -
christopher over 10 yearsI think, in terms of good design, the Comparator should be a completely different class rather than an anonymous object, but I do think that for the context of the question, this answer would suffice.