对String数组列表进行排序

时间:2017-04-28 07:06:02

标签: java arrays list sorting comparator

我收到一个String数组列表

String[] sa1 = {"120", "John" , "30" , "[\"science\",\"arts\"]"};
String[] sa2 = {"20", "Michael" , "29" , "[\"commerce\",\"engineering\"]"};
String[] sa3 = {"20", "Andrew" , "31" , "[\"science\",\"engineering\"]"};

我收到的列表包含这些类型的数组。 我用于排序的当前方法是:

Collections.sort(nameL, new Comparator<String[]>() {

        @Override
        public int compare(String[] o1, String[] o2) {


            return Arrays.toString(o1).compareTo(Arrays.toString(o2));
        }
    });

但是这是基于Ascii字符的排序,因为数组是字符串类型。 问题是我不知道整数的位置

可以是:

sa1 = {"John", "120" , "30" , "[\"science\",\"arts\"]"};

但是如果sa1是这样的话,那么sa2和sa3也会在同一位置有整数

AIM是按照第一个排序,如果是第二个,依此类推,依此类推:

期望的输出

{"20", "Andrew" , "31" , "[\"science\",\"engineering\"]"}
{"20", "Michael" , "29" , "[\"commerce\",\"engineering\"]"},
{"120", "John" , "30" , "[\"science\",\"arts\"]"}

3 个答案:

答案 0 :(得分:1)

你可以试试这样的事情

Comparator<String[]> comparator = Comparator.comparing(array -> {
                                      try {
                                          return Integer.parseInt(array[0]);
                                      } catch (Exception ex) {
                                          return array[0];
                                      }
                                  });

// if all arrays have equal first elements, use the next elements to sort on
String[] firstArray = yourListOfArrays.get(0);
// i starts at 1 because we've used 0 already to create the comparator
for (int i = 1; i < firstArray.length; i++) {
    // i is changing on each iteration, so Java will not allow i
    // to be used inside a lamda
    int index = i;
    comparator.thenComparing(array -> try {
                                          return Integer.parseInt(array[index]);
                                       } catch (Exception ex) {
                                          return array[index];
                             });
}

yourListOfArrays.sort(comparator);

答案 1 :(得分:0)

如果我得到你正确的你需要一个多标准排序,你的比较器的技巧是索引0的元素是表示整数的字符串,所以最好将它们解析为整数

 .test {  }
 .test:first-child {  }

答案 2 :(得分:0)

你可以这样做。 首先找出Integer的位置。参考这个你知道你的String在哪里。然后先按字符串排序,然后按整数排序。

public static void main(String[] args) {

    String[] sa1 = {"John", "120", "30", "[\"science\",\"arts\"]"};
    String[] sa2 = {"Michael", "20", "29", "[\"commerce\",\"engineering\"]"};
    String[] sa3 = {"Andrew", "20", "31", "[\"science\",\"engineering\"]"};
    String[] sa4 = {"Peter", "2020", "31", "[\"science\",\"engineering\"]"};
    String[] sa5 = {"Bob", "99", "31", "[\"science\",\"engineering\"]"};
    String[] sa6 = {"Frank", "333", "31", "[\"science\",\"engineering\"]"};

    List<String[]> nameL = new ArrayList<>();
    nameL.add(sa1);
    nameL.add(sa2);
    nameL.add(sa3);
    nameL.add(sa4);
    nameL.add(sa5);
    nameL.add(sa6);

    int positionInteger = 0;
    int positionString = 0;

    for (int i = 0; i < sa1.length; i++) {
        if (isNumeric(sa1[i])) {
            positionInteger = i;
            if(i == 0){
                positionString = 1;
            }
            break;
        }
    }


    final int pInteger = positionInteger;
    final int pString = positionString;

    Collections.sort(nameL, new Comparator<String[]>() {

        @Override
        public int compare(String[] o1, String[] o2) {
            return o1[pString].compareTo(o2[pString]);
        }
    });


    Collections.sort(nameL, new Comparator<String[]>() {
        public int compare(String[] o1, String[] o2) {

            if(Integer.parseInt(o1[pInteger]) - Integer.parseInt(o2[pInteger]) == 0)
            {
                return 0;
            }

            if(Integer.parseInt(o1[pInteger]) - Integer.parseInt(o2[pInteger]) > 0)
            {
                return 1;
            }

            if(Integer.parseInt(o1[pInteger]) - Integer.parseInt(o2[pInteger]) < 0)
            {
                return -1;
            }

            return 0;
        }
    });

    for (String[] str : nameL) {
      System.out.println(Arrays.toString(str));
    }
}

private static boolean isNumeric(String value)
{
    boolean isNumeric = false;

    try
    {
        int probe = Integer.parseInt(value);
        isNumeric = true;

    }catch(Exception e)
    {
        // do nothing
    }

    return isNumeric;
}