Search code examples
javaseleniumsortingalphanumeric

Alphanumeric column sorting incorrectly


I am trying to sort a list of invoice numbers which contain both letters and digits. When sorting in descending order I would expect Invoice no - 192685 to come before Invoice no - 19s100619916001, however I am getting the opposite. How would i get these into the correct order ? My code for this is below :

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);
}

I have included expected and actual output below. It is a big list but it is only 192685 which is out of place. Please see example below:

Expected output: 1604, 1606, 1609, 1610, 1702, 1733747, 1854, 18687, 18691, 18709, 18710, 18713, 18720, 192685, 19s100619916001, 19s100620272001, 20083461, ]

Actual Output: [1604, 1606, 1609, 1610, 1702, 1733747, 1854, 18687, 18691, 18709, 18710, 18713, 18720, 19s100619916001, 19s100620272001, 192685, 20083461]

The step which I am running is:

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");
    }
}

Which leads onto :

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);
        }
    }
}

Solution

  • It seems that a simple Collections.sort(list) gives you the preferred output.

        List<String> input = Arrays.asList("1604", "1606", "1609", "1610", "1702", "1733747", "1854", "18687", "18691", "18709", "18710", "18713", "18720", "192685", "19s100619916001", "19s100620272001", "20083461");
        Collections.shuffle(input);
        List<String> output = new ArrayList<>(input);
        Collections.sort(output);
        List<String> outputReversed = new ArrayList<>(output);
        Collections.sort(outputReversed, Collections.reverseOrder());
    

    Proof: -

    And the reversed order is working also: enter image description here