字母数字列排序不正确

时间:2018-12-17 14:25:06

标签: java selenium sorting alphanumeric

我正在尝试对包含字母和数字的发票编号列表进行排序。当以降序排序时,我希望发票号-192685会出现在发票号-19s100619916001之前,但是我得到的却恰恰相反。我如何将这些按正确的顺序排列?我的代码如下:

public static void assertSortedAlphaNumerics(Grid grid, String header, boolean isAscending) {
    List<String> original = grid.getColumn(header);
    List<String> originalFormatted = new ArrayList<String>();

    Collections.replaceAll(original, null, "");

    // convert list to lowercase
    original = original.stream().map(String::toLowerCase).collect(Collectors.toList());

    for (int i = 0; i < original.size(); i++) {
        String originalWithoutCharacters = original.get(i).replaceAll(("[^A-Za-z0-9 ]"), "");
        originalFormatted.add(originalWithoutCharacters);
    }

    ArrayList<String> sorted = new ArrayList<String>(originalFormatted);

    if (isAscending) {
        Collections.sort(sorted); // sort ascending
    } else {
        Collections.sort(sorted, Collections.reverseOrder()); // sort descending
    }

    assertEquals("Alphanumeric column is not sorted: " + 
                    header + " expected: \n" +
                    sorted + "\nbut got:\n" + 
                    originalFormatted,
                sorted, 
                originalFormatted);
}

我在下面包括了预期和实际输出。这是一个很大的清单,但是只有192685个地方不合适。请参见下面的示例:

预期输出:1604、1606、1609、1610、1702、1733747、1854、18687、18691、18709、18710、18713、18720、192685、19s100619916001、19s100620272001、20083461,]

实际输出:[1604、1606、1609、1610、1702、1733747、1854、18687、18691、18709、18710、18713、18720、19s100619916001、19s100620272001、192685、20083461]

我正在运行的步骤是:

public void the_table_can_be_sorted(String table) throws Exception {
    switch (table) {
    case "Invoices Awaiting Approval":
        sortInvoicesAwaitingApproval();
        break;

    case "All Jobs":
        sortAllJobs();
        break;

    default:
        throw new Exception("Cannot find table on page");
    }
}

哪个进入:

private void sortInvoicesAwaitingApproval() throws ParseException {
    Grid grid;
    String[] headersInvoicesAwaitingApproval = {"Invoice Ref", "Invoice Date", "Order Ref", "Budget", "Supplier", "Site", "Net ($)", "Tax ($)", "Gross ($)"};
    for (String header : headersInvoicesAwaitingApproval) {
        runtimeState.invoicesAwaitingApprovalPage.tableSort(header, RandomUtils.nextBoolean());
        outputHelper.takeScreenshot();

        grid = runtimeState.invoicesAwaitingApprovalPage.getGrid();

        boolean isAscending = runtimeState.invoicesAwaitingApprovalPage.isAscending(header);
        String order = isAscending ? "ascending" : "descending";
        runtimeState.scenario.write("Asserting " + header + " column is sorted in " + order + " order");

        switch (header){
        case "Invoice Date":
            GridHelper.assertSortedDates(grid, header, "M/d/yyyy", isAscending);
            break;

        case "Net ($)":
        case "Tax ($)":
        case "Gross ($)":
            GridHelper.assertSortedNumerics(grid, header, isAscending);
            break;

        default:
            GridHelper.assertSortedAlphaNumerics(grid, header, isAscending);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

似乎简单的--non-interactive为您提供了首选的输出。

Collections.sort(list)

证明: -

相反的顺序也起作用: enter image description here