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:
- Comparing Strings Not in the Same Order
- One ReplaceAll Statement to Rule Them All!
- A Custom Keyword to Verify a List of Product Categories
- Stripping away characters with ReplaceAll in Katalon Studio
- Calling Another TestCase in Katalon Studio
- Show the currently executing Test Case both before and after it runs
- Marking tests as Passed, Failed or in Error using MarkFailed, MarkPassed, MarkError in Katalon Studio
- Get the xpath of an object in the Object Repository using findPropertyValue(‘xpath’)
- Working with Tables using the Selenium WebDriver and Katalon Studio
- Securely storing passwords and login details with Set Encrypted Text in Katalon Studio