如何创建这个java比较器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20399660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create this java comparator
提问by Fabio Mignogna
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.
@retro:不。description 是一个没有特定格式的字符串。它也可以是空的。
@tobias_k: first "group" by description and than order by priority. priority is always not equals.
@tobias_k:首先按描述“分组”,然后按优先级排序。优先级总是不等于。
@user270349:
@用户270349:
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:
编辑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);
回答by Ruchira Gayan Ranaweera
Test implements Comparator
and override compare()
method
Test implements Comparator
和覆盖compare()
方法
public class Test implements Comparator<Test>{
private int priority;
private String desciption;
@Override
public int compare(Test o1, Test o2) {
// your code here
}
}
回答by christopher
First thing you want to do is make your Test
class implement Comparator
.
您要做的第一件事是让您的Test
班级实现Comparator
.
public class Test implements Comparator<Test> {
// Fields etc.
public int compare(Test test1, Test test2)
{
// Add your logic in here.
return test1.getDescription().compareTo(test2.getDescription()) * -1;
}
}
This will work because String
compares lexographically. It will attempt to sort in ascending order, in terms of the first letter. If the first letter is equal, it will move onto the next value. By reversing it, you can store in descending order.
这将起作用,因为按String
字典顺序进行比较。它将尝试按第一个字母升序排序。如果第一个字母相等,它将移动到下一个值。通过反转它,您可以按降序存储。
Then you sort it, use Collections.sort(myList);
. And here is a working ideone example: http://ideone.com/SdFGFg
然后对它进行排序,使用Collections.sort(myList);
. 这是一个有效的ideone示例:http://ideone.com/SdFGFg
回答by Sage
Comparator<Test> comparator = new Comparator<Test>() {
@Override
public int compare(Test o1, Test o2) {
return o1.desciption.compareTo(o2.desciption);
}
};
As, your problem specifies, you want to order the object of Test
class in descending order with respect to description
field, use Collections.reverseOrder(comparator)
因为,您的问题指定,您想按照字段的Test
降序对类的对象进行排序description
,请使用Collections.reverseOrder(comparator)
Comparator<Test>reverse = Collections.reverseOrder(comparator);
回答by eolith
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);
回答by Prabhakaran Ramaswamy
Do like this
这样做
Your TestComparator
你的测试比较器
class TestComparator implements Comparator<Test> {
@Override
public int compare(Test o1, Test o2) {
String[] arr1 = o1.getDesciption().split("(?<=\D)(?=\d)");
String[] arr2 = o2.getDesciption().split("(?<=\D)(?=\d)");
if(Integer.parseInt(arr2[1])!=Integer.parseInt(arr1[1])){
return new Integer(Integer.parseInt(arr2[1])).compareTo(Integer.parseInt(arr1[1]));
}
if(o1.getPriority()!=o2.getPriority()){
return new Integer(o1.getPriority()).compareTo(o2.getPriority());
}
return 0;
}
}
your sorting
你的排序
System.out.println(list);
Collections.sort(list,new TestComparator());
System.out.println("After sorting");
System.out.println(list);
your output
你的输出
[Test [priority=1, desciption=C2], Test [priority=2, desciption=C1], Test [priority=3, desciption=C1], Test [priority=4, desciption=C2]]
After sorting
[Test [priority=1, desciption=C2], Test [priority=4, desciption=C2], Test [priority=2, desciption=C1], Test [priority=3, desciption=C1]]
回答by aalku
I think you should sort (with comparator), then group, then sort the groups (with another comparator that may use the first comparator applied to the first element on the list), then degroup.
我认为你应该排序(使用比较器),然后分组,然后对组进行排序(使用另一个比较器,可能会使用应用于列表中第一个元素的第一个比较器),然后解组。