简体   繁体   English

按字母顺序对字符串的二维数组进行排序

[英]Sorting 2D array of strings in alphabetical order

Please help, I'm having trouble how to sort Array of Strings in two columns.请帮忙,我在如何对两列中的字符串数组进行排序时遇到了麻烦。 So, I have two columns: Both of it contains a string.所以,我有两列:它们都包含一个字符串。 I need to sort the first column in alphabetical order while the second column should not shuffle, thus, it should correspond to the first column after sorting.我需要按字母顺序对第一列进行排序,而第二列不应该打乱,因此,它应该与排序后的第一列相对应。

Can anyone see where I need to put sorting method in my code below:谁能看到我需要在下面的代码中放置排序方法的位置:

public static void main(String[] args) {
    String[][] emp = {
            {"Victor    ", "ZSA"},
            {"Fred    ", "HAN"},
            {"Drake   ", "SOL"},
            {"Albert  ", "TUR"},
            {"Eric    ", "CAN"}};

    System.out.println("String 1:    String 2: ");
    for (int i = 0; i < emp.length; i++) {
        for (int j = 0; j < emp[0].length; j++) {
            System.out.printf("%9s", emp[i][j]);
        }
        System.out.println();
    }
}

the output should be: output 应该是:

Albert  TUR
Drake   SOL
Eric    CAN
Fred    HAN
Victor  ZSA

You can create a custom comparator implementing the interface Comparator that takes the String[] arrays (array 2D rows) as argument and comparing the first elements of the two arrays like below:您可以创建一个自定义比较器,实现接口Comparator ,它将String[] arrays(数组 2D 行)作为参数并比较两个 arrays 的第一个元素,如下所示:

public class CustomComparator implements Comparator<String[]> {
    @Override
    public int compare(String[] row1, String[] row2) {
        return row1[0].compareTo(row2[0]);
    }
}

After you can execute the sorting in your main method:在您可以在main方法中执行排序之后:

public static void main(String[] args) {
    String[][] emp = {
            {"Victor    ", "ZSA"},
            {"Fred    ", "HAN"},
            {"Drake   ", "SOL"},
            {"Albert  ", "TUR"},
            {"Eric    ", "CAN"}};

    Arrays.sort(emp, new CustomComparator());

    System.out.println("String 1:    String 2: ");
    for (int i = 0; i < emp.length; i++) {
        for (int j = 0; j < emp[0].length; j++) {
            System.out.printf("%9s", emp[i][j]);
        }
        System.out.println();
    }
}

You can use Arrays.sort(T[],Comparator) method as follows:您可以使用Arrays.sort(T[],Comparator)方法如下:

String[][] emp = {
        {"Victor ", "ZSA"},
        {"Fred   ", "HAN"},
        {"Drake  ", "SOL"},
        {"Albert ", "TUR"},
        {"Eric   ", "CAN"}};

// sort by first column alphabetically
Arrays.sort(emp, Comparator.comparing(arr -> arr[0]));

// output
Arrays.stream(emp).map(Arrays::toString).forEach(System.out::println);
[Albert , TUR]
[Drake  , SOL]
[Eric   , CAN]
[Fred   , HAN]
[Victor , ZSA]

See also:也可以看看:
How to sort by a field of class with its own comparator? 如何使用自己的比较器按class 的字段排序?
How to use a secondary alphabetical sort on a string list of names? 如何对名称字符串列表使用二级字母排序?

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

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