如何通过考虑点(。)排序

时间:2017-12-22 12:31:43

标签: java collections

我的rowId如下所示,它将遵循父子关系

<table:Table title="Real-time order status" noDataText="No data to display" id="tableDisp" enableBusyIndicator="true" showNoData="true" width="auto" class="sapUiResponsiveMargin">
                                    <table:Column id="col6" hAlign="Center" headerSpan="[2,1]">
                    <table:multiLabels>
                        <Label text="2 - Batch records handed over to QA"    class="tableHeaderWrap"/>
                        <Label text="Target" textAlign="Center"  />
                    </table:multiLabels>
                    <table:template>
                        <Label text="{Target}"/>
                    </table:template>
                </table:Column>
                <table:Column id="col10" hAlign="Center">
                    <table:multiLabels>
                        <Label text="2 - Batch records handed over to QA"    class="tableHeaderWrap"/>
                        <Label text="Actual" textAlign="Center"/>
                    </table:multiLabels>
                    <table:template>
                        <Label text="{Actual}"/>
                    </table:template>
                </table:Column>

我使用以下代码使用rowid&#39;对bean进行排序

1
1.1
1.1.1
2
2.1
.
.
.
9
9.1
.
9.9
10
10.1

如果我按照上面那样排序,那么它就像下面那样排序

List<MyBean> sortedList = rootItems.stream().sorted(Comparator.comparing(MyBean::getRowId)) .collect(Collectors.toList());

它应该不是这样的。

我想要像我已经给出的rowid的例子那样排序。

有人建议我遵循他的代码......即......,

10
11
12
.
.
19
2
2.1
.
.
3
.
.

如果我按照他的代码,它会返回一个字符串但不返回对象列表..我怎么能使用该代码..请帮助我。

2 个答案:

答案 0 :(得分:3)

您可以比较两个字符串而不实际拆分它们:

int compare(String a, String b) {
  int ai = 0, bi = 0;
  while (ai < a.length() && bi < b.length()) {
    // Extract the next int from a.
    int an = 0;
    while (ai < a.length() && a.charAt(ai) != '.') {
      an = 10*an + Character.getNumericValue(a.charAt(ai));
      ++ai;
    }
    ++ai;  // Skip the dot.

    // Extract the next int from b.
    int bn = 0;
    while (bi < b.length() && b.charAt(bi) != '.') {
      bn = 10*bn + Character.getNumericValue(b.charAt(bi));
      ++bi;
    }
    ++bi;  // Skip the dot.

    // Compare these ints, and return if they're different.
    int cmp = Integer.compare(an, bn);
    if (cmp != 0) return cmp;
  }
  // If we reached the end of one string but not the other,
  // the one we didn't reach the end of is "after" the first.
  if (ai < a.length()) return 1;
  if (bi < b.length()) return -1;
  return 0;
}

Ideone demo

您可以使用此功能通过构建Comparator<MyBean>

对列表中的元素进行排序
List<MyBean> sortedList =
    rootItems.stream()
        .sorted((b1, b2) -> compare(b1.getRowId(), b2.getRowId())
        .collect(Collectors.toList());

答案 1 :(得分:1)

啊也许。

ids需要Comparator才能将其解释为int

public class ChapterComparator {
    // just a simple test
    public static void main(String[] args) {
        List<String> ids = Arrays.asList("10.2", "3.1.1", "10", "1.1", "2", "1");
        Collections.sort(ids, ChapterComparator::compare);
        ids.forEach(System.out::println);
    }

    public static int compare(String o1, String o2) {
        String[] split1 = o1.split("\\."), split2 = o2.split("\\.");
        int result = 0;
        for (int i = 0; i < Math.min(split1.length, split2.length); i++) {
            // compare current segment
            if ((result = Integer.compare(Integer.parseInt(split1[i]), Integer.parseInt(split2[i]))) != 0) {
                return result;
            }
        }
        // all was equal up to now, like "1.1" vs "1.1.1"
        return Integer.compare(split1.length, split2.length);
    };
}

现在,对于您的实际对象,您可以在Comparator中使用comparingBy

List<MyBean> sorted = rootItems.stream()
                          .sorted(Comparator.comparing(MyBean::getRowId, ChapterComparator::compare))
                          .collect(Collectors.toList());

编辑:

和通用版

public static <T> int compareArray(T[] a1, T[] a2, Comparator<T> comparator) {
    int result = 0;
    for (int i = 0; i < Math.min(a1.lengt, a2.length); i++) {
        if (result = comparator.compare(a1[i], a2[i]) != 0) {
            return result;
        }
        return Integer.compare(a1.length, a2.length);
    }
}

你将继续

public static final Comparator<String> COMPARE_IDS = 
    (s1, s2) -> compareArray(s1.split("\\."), s2.split("\\."),
                Comparator.comparing(Integer::parseInt, Integer::compare));

并致电

.sorted(Comparator.comparing(MyBean::getRowId, ChapterComparator.COMPARE_IDS));