使用Comparator对字符串列表进行排序

时间:2018-06-19 08:35:26

标签: java sorting compare comparator

我有字符串。 BOMResult:

1 | 00022954 | 41.418 \ n 2 | 00022951 | 1.0 \ n 3 | 00022945 | 41.575 \ n 3 | 00022944 | 41.684 \ n 3 | 00022944 | 41.778 \ n 3 | 00022944 | 41.871 \ n 3 | 00022946 | 42.918 \ n 3 | 00022944 | 41.918 \ n 3 | 00022944 | 41.825 \ n 3 | 00022944 | 41.731 \ n 3 | 00022945 | 41.621 \ n 3 | 00022953 | 41.512 \ n 4 | 00022957 | 0.0 \ n 5 | 00022947 | 42.809 \ n 5 | 00022942 | 42.918 \ n 5 | 00022948 | 43.918 \ n 5 | 00022947 | 42.871 \ n 5 | 00022950 | 42.746 \ n 4 | 00022952 | 1.0 \ n 5 | 00022941 | 41.246 \ n 5 | 00020472 | 41.184 \ n 2 | 00022958 | 0.0 \ n 3 | 00022945 | 39.621 \ n 3 | 00022944 | 39.731 \ n 3 | 00022944 | 39.84 \ n 3 | 00022944 | 39.949 \ n 3 | 00022944 | 39.887 \ n 3 | 00022944 | 39.793 \ n 3 | 00022945 | 39.684 \ n 3 | 00022956 | 39.512 \ n 4 | 00022959 | 1.0 \ n 5 | 00022941 | 40.762 \ n 5 | 00022943 | 40.699 \ n 4 | 00022957 | 0.0 \ n 5 | 00022947 | 42.809 \ n 5 | 00022942 | 42.918 \ n 5 | 00022948 | 43.918 \ n 5 | 00022947 | 42.871 \ n 5 | 00022950 | 42.746 \ n 3 | 00022949 | 40.996 \ n 3 | 00022944 | 39.996 \ n

enter image description here

当垂直条(|)分隔时,

第一个字符串是level。

最后一个字符串是要对其进行排序的基础。

如果级别相同,请对字符串进行排序。

List<String> rowList = Arrays.asList(BOMResult.split("\n"));
Collections.sort(rowList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        String array1[] = s1.split("\\|");
        String array2[] = s2.split("\\|");
        int i1 = Integer.parseInt(array1[0]);
        int i2 = Integer.parseInt(array2[0]);

        if (i1 == i2) {
            return Double.valueOf(array1[array1.length-1]).compareTo(Double.valueOf(array2[array2.length-1]));
        } else {
            return 0;
        }
    }
});
BOMResult = String.join("\n", rowList);

当前结果:

1|00022954|41.418
2|00022951|1.0
3|00022953|41.512
3|00022945|41.575
3|00022945|41.621
3|00022944|41.684
3|00022944|41.731
3|00022944|41.778
3|00022944|41.825
3|00022944|41.871
3|00022944|41.918
3|00022946|42.918
4|00022957|0.0
5|00020472|41.184
5|00022941|41.246
5|00022950|42.746
5|00022947|42.809
5|00022947|42.871
5|00022942|42.918
5|00022948|43.918
4|00022952|1.0
2|00022958|0.0
3|00022956|39.512
3|00022945|39.621
3|00022945|39.684
3|00022944|39.731
3|00022944|39.793
3|00022944|39.84
3|00022944|39.887
3|00022944|39.949
4|00022957|0.0
4|00022959|1.0
5|00022943|40.699
5|00022941|40.762
5|00022950|42.746
5|00022947|42.809
5|00022947|42.871
5|00022942|42.918
5|00022948|43.918
3|00022944|39.996
3|00022949|40.996

我想在同一级别排序,即使行不同。

预期结果:

1|00022954|41.418
2|00022958|0.0
3|00022956|39.512
3|00022945|39.621
3|00022945|39.684
3|00022944|39.731
3|00022944|39.793
3|00022944|39.84
3|00022944|39.887
3|00022944|39.949
4|00022957|0.0
4|00022959|1.0
5|00022943|40.699
5|00022941|40.762
5|00022950|42.746
5|00022947|42.809
5|00022947|42.871
5|00022942|42.918
5|00022948|43.918
3|00022944|39.996
3|00022949|40.996
2|00022951|1.0
3|00022953|41.512
3|00022945|41.575
3|00022945|41.621
3|00022944|41.684
3|00022944|41.731
3|00022944|41.778
3|00022944|41.825
3|00022944|41.871
3|00022944|41.918
3|00022946|42.918
4|00022957|0.0
5|00020472|41.184
5|00022941|41.246
5|00022950|42.746
5|00022947|42.809
5|00022947|42.871
5|00022942|42.918
5|00022948|43.918
4|00022952|1.0

3 个答案:

答案 0 :(得分:3)

首先为记录创建数据对象
(用实际属性替换名称,即first - &gt; level):

 public static class Data {
    private final int first;
    private final String second;
    private final double third;

    public Data(int first, String second, double third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public int getFirst() {  return first; }
    public String getSecond() { return second; }
    public double getThird() {  return third; }

    @Override
    public String toString() {
        return first + "|" + second + "|" + third;
    }
}

之后,您可以通过链接比较器 排序此类记录的列表。 示例(按第一个排序,然后是第二个,然后是第三个):

List<Data> list = new LinkedList<>();
list.add(new Data(1, "00022954", 41.418));
list.add(new Data(2, "00022951", 1.0));
list.add(new Data(3, "00022953", 41.512));
list.add(new Data(3, "00022945", 41.575));
list.add(new Data(3, "00022945", 41.621));

// shuffle to test sorting
Collections.shuffle(list);

// sort the data
Collections.sort(list, 
    Comparator.comparing(Data::getFirst)
        .thenComparing(Data::getSecond)
        .thenComparing(Data::getThird));

// output the data
list.forEach(System.out::println);

答案 1 :(得分:0)

您可以在同一比较器中合并第一列和最后一列,如下所示:

Collections.sort(rowList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        String array1[] = s1.split("\\|");
        String array2[] = s2.split("\\|");
        Integer i1 = Integer.valueOf(array1[0].trim());
        Integer i2 = Integer.valueOf(array2[0].trim());

        return i1.compareTo(i2) + Double.valueOf(array1[array1.length-1]).compareTo(Double.valueOf(array2[array2.length-1]));

    }
});

答案 2 :(得分:0)

Ok then, try this :

Collections.sort(rowList, new Comparator<String>() {
  @Override
  public int compare(String s1, String s2) {
    String array1[] = s1.split("\\|");
    String array2[] = s2.split("\\|");
    Integer i1 = Integer.valueOf(array1[0].trim());
    Integer i2 = Integer.valueOf(array2[0].trim());
    int res = i1.compareTo(i2);
    if (res == 0) {
      Double d1 = Double.valueOf(array1[array1.length - 1]);
      Double d2 = Double.valueOf(array2[array2.length - 1]);
      return d1.compareTo(d2);
    }
    return res;

  }
});