Search code examples
javaselenium-webdrivertestngrest-assured-jsonpath

How to assert list of string is in sorted order using testng in java?


I am fetching response in sorted order from Get call using "http am.com/au/v/so?sort=name". It fetches the results in sorted order. How can I assert the the name in the list is in alphabetical order.

List<String> list = new ArrayList<String>();
        for (Aml aml : value.getResults()) {
            name = aml.getName());

Solution

  • You can use something like this

    String prev = "";
    for(Ami ami : value.getResults())
    {
      String currentName = ami.getName();
      if(prev.compareTo(currentName) > 0)
      { 
         return false;
      }
      prev = currentName;
    }
    return true;