I have a request which produces below response:
{
\"totalRows\": 10,
\"colDefs\": [
{
\"entityAttributeId\": \"somestring\",
\"headerName
I found the solution by this way:
Store the JSONArray values with the help of jsonpath:
And def temp = $ListDataSet_Response.rowData[*].3
Call the sort functions directly in the feature file itself.
These lines added in feature file Background:
* def ArrayList = Java.type('java.util.ArrayList')
* def Collections = Java.type('java.util.Collections')
Added these line for Sorting in Ascending order:Created list and added values to this temp variable(stored response) into that list.Later,Applied sorting over that list.
And def listAsAscending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsAscending.add(sometextid[i])
* eval Collections.sort(listAsAscending, java.lang.String.CASE_INSENSITIVE_ORDER)
* print listAsAscending
Similarly for Descending order:
And def listAsDescending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsDescending.add(sometextid[i])
* eval Collections.sort(listAsDescending, Collections.reverseOrder())
* print listAsDescending
To compare the list, with my sort API response later:
* match sortedTemp == listAsAscending
* match sortedTemp == listAsDescending
Please refer to this example that even involves a case-insensitive sort. It is easy to dive into Java from Karate, especially for a complex use-case like this:
Scenario: case-insensitive sort
* def ArrayList = Java.type('java.util.ArrayList')
* def Collections = Java.type('java.util.Collections')
* def json = [{ v: 'C' }, { v: 'b' }, { v: 'A' }]
* def actual = $json[*].v
* match actual == ['C', 'b', 'A']
* def list = new ArrayList()
* eval for (var i = 0; i < actual.length; i++) list.add(actual[i])
* match list == ['C', 'b', 'A']
* eval Collections.sort(list, java.lang.String.CASE_INSENSITIVE_ORDER)
* match list == ['A', 'b', 'C']
Since your example is too complex, I can't provide a specific answer, kindly refer: https://stackoverflow.com/help/mcve
For a cleaner way to express the above code, refer the actual example, where you can move some of the code into JS: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/sort-array.feature