# Testing data that has been sorted

Can people share ways in which they have tested the sorting of data. I don’t want to assert on the data itself as this would cause the tests to be flaky and dependent on the data not changing and always being available.

An example of my scenario. I have a table with a column called ‘Date Received’. I’d like to assert that the items in the table are initially ordered by newest to oldest (ascending order).

• Get the count of the rows in the tables
• Iterate over each row and retrieve the column values (in this case, just the date value). Then add then column value to a list. When the iteration has complete, I will have a list of dates in the order then are displayed in the table
• Next I will order the list by ascending order and assign it to a new list variable
• Finally, I will compare the 2 list to ensure the items are in the correct order.

My pseudo approach above seems a little long winded. Can anyone share a more simpler approach on asserting the sorting on data in Katalon?

I agree, it does seem like you can simplify the approach, and good on you for wanting to!

I’ve done this exact same thing, and here’s how I approached it:

The trick is to make use of the compareTo() method of the Date class:

``````Date myDate = ...
Date anotherDate = ...
int comparison = myDate.compareTo(anotherDate)
``````

The value of `comparison` will be an integer value with the following logic:

• If `myDate` is equal to `anotherDate`, the value of `comparison` will be 0.
• If `myDate` is before `anotherDate`, the value of `comparison` will be a negative integer.
• If `myDate` is after `anotherDate`, the value of `comparison` will be a positive integer.

Let’s assume that your dates are in “MM/dd/yyyy HH:mm” format (you can change the format to suite your needs, see SimpleDateFormat). Then the following code should do what you need in an efficient way:

``````int rowCount = ...
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm")
for(int i = 2; i <= rowCount; i++) {
String previousCellText = ... // get text from row i - 1
String currentCellText = ... // get text from row i
Date previousDate = format.parse(previousCellText)
Date currentDate = format.parse(currentCellText)
assert currentDate.compareTo(previousDate) >= 0
}
``````

Anywhere you see an ellipses ("…") in the above code you will need to replace with your method of retrieving values from the page.

For getting the cell from each row, note that we are using the loop index to indicate which row we are looking at during any given iteration. `previousCellText` will always look at row `i - 1`, and `currentCellText` at row `i`.

Let me know if this makes sense. Hopefully it will work for you

2 Likes

Fantastic response. The compareTo method helped me with testing the sorting for the rest of the columns.

Here’s my take:

1. Go to https://the-internet.herokuapp.com/tables page
2. Click the “Last Name” header on the first table in order to sort the table
3. See if it is sorted by comparing the UI values with `Collections.sort()` method
``````WebUI.openBrowser("https://the-internet.herokuapp.com/tables")
WebDriver driver = DriverFactory.getWebDriver()
driver.findElement(By.xpath("//table[@id='table1']//span[contains(.,'Last Name')]")).click()

List<WebElement> tableElements = driver.findElements(By.cssSelector("#table1 tr td:nth-child(1)"));
ArrayList<String> tableValues = new ArrayList<String>();
for(int i=0; i < tableElements.size(); i++){
String str = tableElements.get(i).getText();
}

ArrayList<String> referenceValues = new ArrayList<String>();
for(int i=0; i < tableValues.size(); i++){
}

Collections.sort(referenceValues)

assert referenceValues.equals(tableValues)
``````
2 Likes

Note, this is much quicker than my solution

I remember trying something similar, but I don’t remember if Collections.sort() works for dates?

Not sure, it works with strings. But I am sure something similar could be made with dates.

Hi,
@ kazurayam @Brandon_Hein could you pls help here
I tried collections.sort() but i could see the value returned is having some special characters. So, it is failing at assert step.

From UI, my data is like [00MKC,etc]
but the reference values after sorting, it is giving me as values as [,00MKC]

ArrayList tableValues = new ArrayList()

println(rows_count)-> 11

for (int row = 0; row < rows_count; row++) {

`````` String str = tableElements.get(row).getText();
``````

}

ArrayList referenceValues = new ArrayList();

println(tableValues.size())
for(int i = 0; i < tableValues.size(); i++){

`````` referenceValues.add(tableValues.get(i))
``````

}

Collections.sort(referenceValues)
assert referenceValues.equals(tableValues)

@eug How about not allowing any of the non-desired characters into the table? Use the replace method to change the comma to blank and then not allow blanks into the table.

String str = tableElements.get(row).getText();
str = str.replaceAll(",","");
if (str.length() > 0) {
}

What this does is removes the comma from being added to the table.You can add multiple replace methods to remove different characters.

If you copy and paste, you have to change all smart quotes–have a curve or curl to them-with straight quotes.

I need an concrete example of your data to sort. Please change the following snippet to make it look like your case.

``````def dataset = ["abc", "de", "fghi", "jk", "lmno", "pqrsu", "vw", "xy", "z"]
``````

def dataset = [,“abc”, “de”, “fghi”, “jk”, “lmno”, “pqrsu”, “vw”, “xy”, “z”]

after collections.sort() it gives result like above. I am comparing whether the data received from UI is in ascending order.

@kazurayam def dataset = [,“abc”, “de”, “fghi”, “jk”, “lmno”, “pqrsu”, “vw”, “xy”, “z”]

there are multiple 11 commas in front of first data “abc”
after collections.sort() it gives result like above. I am comparing whether the data received from UI is in ascending order.

Sorry, my last question was useless. It does not help understand your original problem.

Let me ask you again. I need to reproduce your problem on my PC. I need to see the example set of string you want to sort, full lines of your test script, what you expect to see, and what you actually get.