简体   繁体   English

如何根据两个值对数组进行排序?

[英]How to sort an array of pairs according to both values?

I am trying to sort an array that consists of strings of integer pairs using Java我正在尝试使用 Java 对包含 integer 对的字符串进行排序

The input is:输入是:

["2,0", "1,3", "2,3", "1,0", "6,4", "5,7", "10,8", "7,11", "9,11", "11,12"]

The output needed is:所需的 output 是:

["1,0", "1,3", "2,0", "2,3", "5,7", "6,4", "7,11", "9,11", "10,8", "11,12"]

Use a comparator which first reads the string before the comma, parses it to an int, and compares;使用比较器,它首先读取逗号之前的字符串,将其解析为 int,然后进行比较; then a comparator which does the same, but for the part of the string after the comma:然后是一个比较器,它做同样的事情,但对于逗号后面的字符串部分:

Comparator<String> pairComparator =
    Comparator.comparingInt(s -> Integer.parseInt(s.substring(0, s.indexOf(','))))
          .thenComparingInt(s -> Integer.parseInt(s.substring(s.indexOf(',') + 1)));

Try this.尝试这个。

List<String> list = List.of("2,0", "1,3", "2,3", "1,0", "6,4", "5,7", "10,8", "7,11", "9,11", "11,12");
List<String> output = list.stream()
    .map(e -> new Object() {
        int[] a = Stream.of(e.split(",")).mapToInt(Integer::parseInt).toArray();
        String origin = e;
    })
    .sorted((obj1, obj2) -> Arrays.compare(obj1.a, obj2.a))
    .map(obj -> obj.origin)
    .collect(Collectors.toList());
System.out.println(output);

output output

[1,0, 1,3, 2,0, 2,3, 5,7, 6,4, 7,11, 9,11, 10,8, 11,12]

Old method using loops使用循环的旧方法

        String[] values = {"2,0", "1,3", "2,3", "1,0", "6,4", "5,7", "10,8", "7,11", "9,11", "11,12"};
        Integer[][] pairs = new Integer[10][2];
        String e[] = null;
        for(int i = 0; i < values.length; i++) {   // To extract the integer values
            e = values[i].split(",");
            pairs[i][0] = Integer.parseInt(e[0]);
            pairs[i][1] = Integer.parseInt(e[1]);       
         }      
        Integer temp1;
        Integer temp2;
        
        for(int i = 0; i < pairs.length; i++) {   // n^2 pass
            for( int j= 0; j < pairs.length; j ++) {
                
                if (pairs[i][0] < pairs[j][0]) {
                    
                    temp1 = pairs[j][0];
                    temp2 = pairs[j][1];
                    
                    pairs[j][0] = pairs[i][0];
                    pairs[j][1] = pairs[i][1];
                    
                    pairs[i][0] = temp1;
                    pairs[i][1] = temp2;    
                }
                else if (pairs[i][0] == pairs[j][0]) {
                    
                    if (pairs[i][1] != pairs[j][1]) {;
                        temp2 = pairs[j][1];
                        
                        pairs[j][1] = pairs[i][1];
                        
                        pairs[i][1] = temp2;
                        }
                    
                }
                
              } // inner for loop
        } // outer for loop
        
        for(int i = 0; i < pairs.length; i++) {
            System.out.println(pairs[i][0]+","+pairs[i][1]);
        }

Output Output

1,0
1,3
2,3
2,0
5,7
6,4
7,11
9,11
10,8
11,12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM