# Testing data that has been sorted

**URL:** <https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 25, 2019, 4:23pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785 "2019-11-25T16:23:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph.aspey93](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/joseph.aspey93/32/16104_2.png) [@joseph.aspey93](https://forum.katalon.com/u/joseph.aspey93)\
**Post date:** [November 25, 2019, 4:23pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/1 "2019-11-25T16:23:52Z")

</div>

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).

I had an idea to;

- 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?

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [November 25, 2019, 5:21pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/2 "2019-11-25T17:21:34Z")

</div>

> [@joseph.aspey93](#):
>
> 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!

> [@joseph.aspey93](#):
>
> 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).

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](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#compareTo-java.util.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](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)). 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 🙂

---

<div class="post-metadata">

**Author:** ![joseph.aspey93](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/joseph.aspey93/32/16104_2.png) [@joseph.aspey93](https://forum.katalon.com/u/joseph.aspey93)\
**Post date:** [November 26, 2019, 11:20am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/3 "2019-11-26T11:20:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [November 26, 2019, 1:27pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/4 "2019-11-26T13:27:51Z")

</div>

Here’s my take:

1. Go to [https://the-internet.herokuapp.com/tables](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

```auto
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();
    tableValues.add(str);
}

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

Collections.sort(referenceValues)

assert referenceValues.equals(tableValues)

```

---

<div class="post-metadata">

**Author:** ![joseph.aspey93](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/joseph.aspey93/32/16104_2.png) [@joseph.aspey93](https://forum.katalon.com/u/joseph.aspey93)\
**Post date:** [November 26, 2019, 3:42pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/5 "2019-11-26T15:42:18Z")

</div>

Note, this is much quicker than my solution

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [November 26, 2019, 4:41pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/6 "2019-11-26T16:41:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [November 27, 2019, 7:42am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/7 "2019-11-27T07:42:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eug](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@eug](https://forum.katalon.com/u/eug)\
**Post date:** [July 15, 2020, 9:06am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/8 "2020-07-15T09:06:15Z")

</div>

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]

Please help me how to remove this special character.

ArrayList tableValues = new ArrayList()

println(rows\_count)-\> 11

for (int row = 0; row \< rows\_count; row++) {

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

```

}

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)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [July 16, 2020, 3:37pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/9 "2020-07-16T15:37:10Z")

</div>

@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) {**  
tableValues.add(str);  
**}**

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

> <https://stackoverflow.com/questions/43719805/replace-multiple-characters-in-string-with-multiple-different-characters>

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

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 16, 2020, 8:16pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/10 "2020-07-16T20:16:17Z")

</div>

> [@eug](#):
>
> 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]

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

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

```

---

<div class="post-metadata">

**Author:** ![eug](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@eug](https://forum.katalon.com/u/eug)\
**Post date:** [July 21, 2020, 5:29am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/11 "2020-07-21T05:29:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eug](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@eug](https://forum.katalon.com/u/eug)\
**Post date:** [July 21, 2020, 5:30am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/12 "2020-07-21T05:30:59Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [July 21, 2020, 5:34am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/13 "2020-07-21T05:34:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![githa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/githa/32/23870_2.png) [@githa](https://forum.katalon.com/u/githa)\
**Post date:** [October 18, 2023, 5:23am UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/14 "2023-10-18T05:23:23Z")

</div>

hi I try it but I always got error

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [October 18, 2023, 2:42pm UTC](https://forum.katalon.com/t/testing-data-that-has-been-sorted/36785/15 "2023-10-18T14:42:09Z")

</div>

Whenever you get an error, **if you want anyone to assist you** , then you need to either tell us about the error, give us the log that contains the error or tell us the information that you know because you can see the error but we can’t.  
How about reading [this](http://forum.katalon.com/t/tip-how-to-help-us-help-you/12919/43) and go from there?
