如何在Java中根据另一个数组进行排序,当元素重复时?

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

How to sort one array based on another, when elements repeat, in Java?

问题

以下是翻译好的部分:

我有两个ArrayList

A:5 3 2 6 1 4

B:0 1 2 0 3 2

我想根据A中的相应值对B进行排序,所以我应该得到:3 2 1 2 0 0

当我使用以下代码时:

  1. ArrayList<Integer> D=new ArrayList<Integer>(B);
  2. Collections.sort(B, Comparator.comparing(s -> A.get(D.indexOf(s))));

或者:

  1. ArrayList<Integer> D = new ArrayList<Integer>(B);
  2. Collections.sort(B, new Comparator<Integer>(){
  3. public int compare(Integer a,Integer b){
  4. return Integer.compare(A.get(D.indexOf(a)),A.get(D.indexOf(b)));
  5. }
  6. });

如果B中的元素是唯一的,那么这将起作用,但由于2和0都出现了2次,每次调用A.get(D.indexOf(2))时,都会返回2,而4永远不会返回。

所以我最终得到:3 2 2 1 0 0

有人可以帮我解决这个问题吗?我不想编写完整的排序算法,但这样的解决方案也是可以的。

英文:

I have two ArrayLists

A: 5 3 2 6 1 4

B: 0 1 2 0 3 2

I want to sort B, based on corresponding values from A, so I should get : 3 2 1 2 0 0

When I use the following codes :

  1. ArrayList&lt;Integer&gt; D=new ArrayList&lt;Integer&gt;(B);
  2. Collections.sort(B, Comparator.comparing(s -&gt; A.get(D.indexOf(s))));

or :

  1. ArrayList&lt;Integer&gt; D = new ArrayList&lt;Integer&gt;(B);
  2. Collections.sort(B, new Comparator&lt;Integer&gt;(){
  3. public int compare(Integer a,Integer b){
  4. return Integer.compare(A.get(D.indexOf(a)),A.get(D.indexOf(b)));
  5. }
  6. });

It would have worked if the elements in B were unique, but since both 2 and 0 has 2 occurances, every time A.get(D.indexOf(2)) is called, 2 is returned and 4 is never returned.

So I finally get : 3 2 2 1 0 0

Can anyone help me with a comparator that deals with this? I don't want to make a full sorting algorithm, but such solutions are also welcome.

答案1

得分: 2

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Main {
  4. public static void main(String[] args) {
  5. List<Integer> list1 = List.of(5, 3, 2, 6, 1, 4);
  6. List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
  7. List<Integer> tempList = new ArrayList<>();
  8. for (Integer i : list1) {
  9. tempList.add(list2.get(i - 1));
  10. }
  11. System.out.println(tempList);
  12. }
  13. }
  14. **Output:**
  15. [3, 2, 1, 2, 0, 0]
  16. **[Update]**
  17. Given below is an updated solution to meet OP's new requirement mentioned in the comment below the answer.
  18. ```java
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;
  22. public class Main {
  23. public static void main(String[] args){
  24. List<Integer> list1 = List.of(15, 13, 12, 16, 11, 14);
  25. List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
  26. List<Integer> tempList = new ArrayList<>();
  27. int max = Collections.max(list1);
  28. int offset = max - list2.size() + 1;
  29. for (Integer i : list1) {
  30. tempList.add(list2.get(i - offset));
  31. }
  32. System.out.println(tempList);
  33. }
  34. }
  35. **Output:**
  36. [3, 2, 1, 2, 0, 0]
英文:

A simple way to do it as follows:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Main {
  4. public static void main(String[] args) {
  5. List&lt;Integer&gt; list1 = List.of(5, 3, 2, 6, 1, 4);
  6. List&lt;Integer&gt; list2 = List.of(0, 1, 2, 0, 3, 2);
  7. List&lt;Integer&gt; tempList = new ArrayList&lt;&gt;();
  8. for (Integer i : list1) {
  9. tempList.add(list2.get(i - 1));
  10. }
  11. System.out.println(tempList);
  12. }
  13. }

Output:

  1. [3, 2, 1, 2, 0, 0]

[Update]

Given below is an updated solution to meet OP's new requirement mentioned in the comment below the answer.

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. public class Main {
  5. public static void main(String[] args){
  6. List&lt;Integer&gt; list1 = List.of(15, 13, 12, 16, 11, 14);
  7. List&lt;Integer&gt; list2 = List.of(0, 1, 2, 0, 3, 2);
  8. List&lt;Integer&gt; tempList = new ArrayList&lt;&gt;();
  9. int max = Collections.max(list1);
  10. int offset = max - list2.size() + 1;
  11. for (Integer i : list1) {
  12. tempList.add(list2.get(i - offset));
  13. }
  14. System.out.println(tempList);
  15. }
  16. }

Output:

  1. [3, 2, 1, 2, 0, 0]

huangapple
  • 本文由 发表于 2020年4月7日 07:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070688.html
匿名

发表评论

匿名网友

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

确定