You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修正动态表格单双位数数字的升序排序验证问题

Fixing Numeric Sorting Issue for Dynamic Tables (Single/Double Digit Numbers)

Hey there, the problem you're facing is a classic case of lexicographical (string) sorting vs. numeric sorting. When you use String.compareTo(), it compares characters one by one based on their Unicode values—so "8" comes before "22" because the character '8' has a higher Unicode value than '2'. That's why your sorted output looks messed up for multi-digit numbers.

Here's how to fix this step by step:

1. Store Values as Numbers, Not Strings

Instead of saving the table cell text as strings, convert them directly to integers (or longs if dealing with very large numbers). This way, we can sort by actual numeric value.

2. Adjust Sorting Logic for Numeric Values

Use Java's collection sorting utilities designed for numbers, not strings.

3. Validate Against Numeric Values

When checking if the table is sorted correctly, compare the numeric values instead of raw strings.


Corrected Code Example

// Step 1: Extract numeric values from table cells and store in an Integer list
List<Integer> originalNumbers = new ArrayList<>();
List<WebElement> numberCells = driver.findElements(By.xpath("//mat-table//mat-row/mat-cell[2]"));

// Only locate elements once (no need to re-find in the loop!)
for (WebElement cell : numberCells) {
    String cellText = cell.getText().trim();
    Reporter.log(AddRule + " Column as per display order " + cellText);
    Add_Log.info(AddRule + " Column as per display order " + cellText);
    
    // Convert text to integer and add to list
    originalNumbers.add(Integer.parseInt(cellText));
}

// Step 2: Create a sorted copy of the list
// For ASCENDING order:
List<Integer> sortedAscending = new ArrayList<>(originalNumbers);
Collections.sort(sortedAscending);

// For DESCENDING order (if you need to validate that instead):
// List<Integer> sortedDescending = new ArrayList<>(originalNumbers);
// Collections.sort(sortedDescending, Collections.reverseOrder());

// Print sorted values for debugging
System.out.println("##################Sorted values in the Array and compare order####################");
for (Integer num : sortedAscending) {
    System.out.println(num);
}

// Step 3: Validate the table's order against the sorted list
boolean isSortedCorrectly = true;
for (int i = 0; i < originalNumbers.size(); i++) {
    int actualNumber = Integer.parseInt(numberCells.get(i).getText().trim());
    int expectedNumber = sortedAscending.get(i);
    
    if (actualNumber != expectedNumber) {
        String errorMsg = String.format("%s Column is not in order. Expected: %d, Actual: %d", AddRule, expectedNumber, actualNumber);
        Reporter.log(errorMsg);
        Add_Log.info(errorMsg);
        isSortedCorrectly = false;
        break; // Stop checking further once a mismatch is found
    } else {
        String successMsg = String.format("%s %d Column is displayed in ascending order", AddRule, actualNumber);
        Reporter.log(successMsg);
        Add_Log.info(successMsg);
    }
}

// Fail the assertion if sorting is incorrect
if (!isSortedCorrectly) {
    Assert.fail(AddRule + " Column is not sorted in ascending order as expected");
}

Key Improvements & Notes

  • Avoid Repeated Element Locating: Your original code called findElements inside the loop—this is inefficient and can cause issues if the DOM changes. We locate elements once and reuse the list.
  • Handle Edge Cases: If your table cells have non-numeric characters (like "Item 12"), you'll need to extract just the numeric part first (e.g., using regex: cellText.replaceAll("[^0-9]", "")).
  • Large Numbers: If your values exceed the Integer range, switch to Long.parseLong() instead of Integer.parseInt().

内容的提问来源于stack exchange,提问作者ASSS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:42:39