英文:
Java sort array of objects by integer field and if those are identical then sort by another integer value
问题
我在处理一个涉及对对象数组进行排序的 Java 问题。
我已经解决了按特定字段对对象数组进行排序的方法,可以在以下代码中看到:
public void printPrioritized() {
System.out.println("Prioritized todo:");
System.out.println("-----------------");
List<Task> sortedList = new ArrayList<Task>(taskList);
Collections.sort(sortedList, new Comparator<Task>() {
public int compare(Task o1, Task o2) {
int priorityComparison = Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
if (priorityComparison == 0) {
// If priorities are the same, sort by another value (e.g., time)
return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
}
return priorityComparison;
}
});
sortedList.forEach((e) -> {
System.out.println(e);
});
}
我的问题是,如果两个对象的某些字段相同,那么我应该按另一个值进行排序。这意味着我需要按 1 到 4 的值进行排序(通过 getPriority()
方法),但如果两个对象例如都是 2,则我必须按另一个值排序,比如时间。希望有人能够帮助。
英文:
I have a java problem in relation too sorting an arrayList of objects.
I have already figured out to sort an arrayList of object by specific fields, which can be seen in the following code
public void printPrioritized() {
System.out.println("Prioritized todo:");
System.out.println("-----------------");
List<Task> sortedList = new ArrayList<Task>(taskList);
Collections.sort(sortedList, new Comparator<Task>() {
public int compare(Task o1, Task o2) {
return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
}
});
sortedList.forEach((e) -> {
System.out.println(e);
});
My problem is that if to object fields are the same then i am supposed to sort by another value. This means that i have to sort by an value of 1 to 4 (getPriority() method), but if two objects for instance both are 2 then i have to sort by another value which for instance could be time. Hope someone can help.
答案1
得分: 3
假设你的 Task
类看起来类似于:
class Task {
int priority;
int anotherValue;
// 获取器、设置器...
}
你可以创建自定义比较器并在排序时链式使用它们,示例如下:
List<Task> myList = new ArrayList<>();
Comparator<Task> byPriority = (t1, t2) -> Integer.compare(t1.getPriority(), t2.getPriority());
Comparator<Task> byAnotherValue = (t1, t2) -> Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
myList.sort(byPriority.thenComparing(byAnotherValue));
或者你可以将这些排序组合起来,如下所示;
List<Task> myList = new ArrayList<>();
Comparator<Task> sortedComparator = (t1, t2) -> {
if (t1.getPriority() != t2.getPriority()) {
return Integer.compare(t1.getPriority(), t2.getPriority());
} else if (t1.getAnotherValue() != t2.getAnotherValue()) {
return Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
}
};
myList.sort(sortedComparator);
英文:
Assuming your Task
class looks something like:
class Task {
int priority;
int anotherValue;
// getters, setters ...
}
you can create custom compartors and chain them while sorting, example:
List<Task> myList = new ArrayList<>();
Comparator<Task> byPriority = (t1,t2) -> Integer.compare(t1.getPriority(), t2.getPriority());
Comparator<Task> byAnotherValue = (t1,t2) -> Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
myList.sort(byPriority.thenComparing(byAnotherValue));
OR
you can combine those sortings ->
List<Task> myList = new ArrayList<>();
Comparator<Task> sortedComparator = (t1,t2) -> {
if (t1.getPriority() != t2.getPriority()) {
return Integer.compare(t1.getPriority(), t2.getPriority());
}
else if (t1.getAnotherValue() != t2.getAnotherValue()) {
return Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
}
};
myList.sort(sortedComparator);
答案2
得分: 2
尝试自定义compare
方法。
例如:
if(o1.getPriority() != o2.getPriority())
return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
if(o1.getTime() != o2.getTime())
return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
return 0; //它们在所有字段上都相等
英文:
Try to customize the compare
method.
e.g.
if(o1.getPriority() != o2.getPriority())
return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
if(o1.getTime() != o2.getTime())
return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
return 0; //they are equal with all fields
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论