Java sort array of objects by integer field and if those are identical then sort by another integer value

huangapple go评论102阅读模式
英文:

Java sort array of objects by integer field and if those are identical then sort by another integer value

问题

我在处理一个涉及对对象数组进行排序的 Java 问题。

我已经解决了按特定字段对对象数组进行排序的方法,可以在以下代码中看到:

  1. public void printPrioritized() {
  2. System.out.println("Prioritized todo:");
  3. System.out.println("-----------------");
  4. List<Task> sortedList = new ArrayList<Task>(taskList);
  5. Collections.sort(sortedList, new Comparator<Task>() {
  6. public int compare(Task o1, Task o2) {
  7. int priorityComparison = Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
  8. if (priorityComparison == 0) {
  9. // If priorities are the same, sort by another value (e.g., time)
  10. return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
  11. }
  12. return priorityComparison;
  13. }
  14. });
  15. sortedList.forEach((e) -> {
  16. System.out.println(e);
  17. });
  18. }

我的问题是,如果两个对象的某些字段相同,那么我应该按另一个值进行排序。这意味着我需要按 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

  1. public void printPrioritized() {
  2. System.out.println(&quot;Prioritized todo:&quot;);
  3. System.out.println(&quot;-----------------&quot;);
  4. List&lt;Task&gt; sortedList = new ArrayList&lt;Task&gt;(taskList);
  5. Collections.sort(sortedList, new Comparator&lt;Task&gt;() {
  6. public int compare(Task o1, Task o2) {
  7. return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
  8. }
  9. });
  10. sortedList.forEach((e) -&gt; {
  11. System.out.println(e);
  12. });

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 类看起来类似于:

  1. class Task {
  2. int priority;
  3. int anotherValue;
  4. // 获取器、设置器...
  5. }

你可以创建自定义比较器并在排序时链式使用它们,示例如下:

  1. List<Task> myList = new ArrayList<>();
  2. Comparator<Task> byPriority = (t1, t2) -> Integer.compare(t1.getPriority(), t2.getPriority());
  3. Comparator<Task> byAnotherValue = (t1, t2) -> Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
  4. myList.sort(byPriority.thenComparing(byAnotherValue));

或者你可以将这些排序组合起来,如下所示;

  1. List<Task> myList = new ArrayList<>();
  2. Comparator<Task> sortedComparator = (t1, t2) -> {
  3. if (t1.getPriority() != t2.getPriority()) {
  4. return Integer.compare(t1.getPriority(), t2.getPriority());
  5. } else if (t1.getAnotherValue() != t2.getAnotherValue()) {
  6. return Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
  7. }
  8. };
  9. myList.sort(sortedComparator);
英文:

Assuming your Task class looks something like:

  1. class Task {
  2. int priority;
  3. int anotherValue;
  4. // getters, setters ...
  5. }

you can create custom compartors and chain them while sorting, example:

  1. List&lt;Task&gt; myList = new ArrayList&lt;&gt;();
  2. Comparator&lt;Task&gt; byPriority = (t1,t2) -&gt; Integer.compare(t1.getPriority(), t2.getPriority());
  3. Comparator&lt;Task&gt; byAnotherValue = (t1,t2) -&gt; Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
  4. myList.sort(byPriority.thenComparing(byAnotherValue));

OR
you can combine those sortings ->

  1. List&lt;Task&gt; myList = new ArrayList&lt;&gt;();
  2. Comparator&lt;Task&gt; sortedComparator = (t1,t2) -&gt; {
  3. if (t1.getPriority() != t2.getPriority()) {
  4. return Integer.compare(t1.getPriority(), t2.getPriority());
  5. }
  6. else if (t1.getAnotherValue() != t2.getAnotherValue()) {
  7. return Integer.compare(t1.getAnotherValue(), t2.getAnotherValue());
  8. }
  9. };
  10. myList.sort(sortedComparator);

答案2

得分: 2

尝试自定义compare方法。
例如:

  1. if(o1.getPriority() != o2.getPriority())
  2. return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
  3. if(o1.getTime() != o2.getTime())
  4. return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
  5. return 0; //它们在所有字段上都相等
英文:

Try to customize the compare method.
e.g.

  1. if(o1.getPriority() != o2.getPriority())
  2. return Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
  3. if(o1.getTime() != o2.getTime())
  4. return Integer.valueOf(o1.getTime()).compareTo(o2.getTime());
  5. return 0; //they are equal with all fields

huangapple
  • 本文由 发表于 2020年10月20日 02:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64432844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定