Sorting in Ascending and Descending order with .toStorted() and .reverse()
Until now my validation of sorted objects has been quite minimal. I’ve gotten by with very simple tests such as IF A>B, IF B>C. However, there is a much better and simpler way to check sorting using the Groovy functions of .toSorted().
To make proper use of this, I would read values from a table into a my own List. I would then duplicate that List, sort it, then compare if the two were a match to each other.
However, .toSorted() returns a list in Ascending order. While this is extremely handy, how do you get the List in Descending order?
A second command, .reverse() can be used to quite literally, reverse the list. Since the list is already sorted Ascending, the reverse of that would be Descending.
In combination with a sorted list it’s not uncommon to need to remove duplicates. The .toUnique() command does just that. The sort of the List is maintained with the duplicate items remove. The List now has the unique items available.
Listed below are a couple of examples to illustrate how each works. Three Lists are created, one with numbers, another with food names and a final with repeated values. The first two are sorted so as to be in Ascending order. The List is then reversed to make it Descending. For the repeated List, the duplicate entries are remove so that the unique spellings for “to” and “for” are kept.
//Lists numberList = [87453, 974639, 6394, 187298, 435, 205735, 18, 43298, 11340625] nameList=['Tangerine','Orange','Carrot','Watermelon','Pear','Grape','Banana'] repeatedList=['to','too','two','for','four','fore','too','four','four','for','two'] //Sort Ascending numberList=numberList.toSorted() nameList=nameList.toSorted() log.logWarning('Ascending:=' + numberList) log.logWarning('Ascending:=' + nameList) //Sort Descending numberList=numberList.reverse() nameList=nameList.reverse() log.logWarning('Descending:=' + numberList) log.logWarning('Descending:=' + nameList) //Unique List repeatedList=repeatedList.toUnique() log.logWarning('Unique List:=' + repeatedList) Output: Ascending:=[18, 435, 6394, 43298, 87453, 187298, 205735, 974639, 11340625] Ascending:=[Banana, Carrot, Grape, Orange, Pear, Tangerine, Watermelon] Descending:=[11340625, 974639, 205735, 187298, 87453, 43298, 6394, 435, 18] Descending:=[Watermelon, Tangerine, Pear, Orange, Grape, Carrot, Banana] Unique List:=[to, too, two, for, four, fore]
Other articles of interest:
- One ReplaceAll Statement to Rule Them All!
- Working with Tables using the Selenium WebDriver and Katalon Studio
- Getting the number of pages in a pagination ribbon
- Stripping away characters with ReplaceAll in Katalon Studio
- Show the currently executing Test Case both before and after it runs
- Calling Another TestCase in Katalon Studio
- Output status messages and test information by writing to the Log File Viewer in Katalon Studio
- Marking tests as Passed, Failed or in Error using MarkFailed, MarkPassed, MarkError in Katalon Studio
- Getting the Length of a String and the Size of a List
- Selenium based Custom Keyword to Count Pagination in Katalon Studio