ArrayList<String> actualList = new ArrayList<>();
List<WebElement> elementList= driver.findElements(By.xpath(""));
for(webElement we:elementList){
actualList.add(we.getText);
}
ArrayList<String> sortedList = new ArrayList<>();
for(String s:obtainedList){
sortedList.add(s);
}
Collections.sort(sortedList);
Assert.assertTrue(sortedList.equals(obtainedList));
For reverse sorting:
Collections.reverse(sortedList);