简体   繁体   中英

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

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:

["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; 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

[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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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