Syed
Syed

Reputation: 187

how to verify if the elements in the list is alphabetically sorted or not

I am using the following code to get the text values for employees from a web table and holding it in a list.Now i need to verify the employees stored in the list are in alphabetical order or not.

  int count = a.getobjectcount(//*[@id='GridTable']/tbody/tr[*]);
    List< String> list = new List<String>();
    for(int i=0;i<=count-1;i++){
    a.getTextFromElement("//*[@id='emp_" +i+ "']");

    //Using for loop to get the number of employees and store it in a list

 list.add(element)// i am adding employees to the list here   
  }

Here i need to validate if employees is in alphabetical order or not something as boolean==true; if employees are in alphabetical order

Upvotes: 1

Views: 2912

Answers (3)

Syed
Syed

Reputation: 187

The following code worked for me for sorting validation

public Boolean validateSorting(){
int count = a.getobjectcount(//*[@id='GridTable']/tbody/tr[*]);
    List< String> list = new List<String>();
    for(int i=0;i<=count-1;i++){
    a.getTextFromElement("//*[@id='emp_" +i+ "']");

    //Using for loop to get the number of employees and store it in a list

 list.add(element)// i am adding employees to the list here   
  }
 var y = list.First();
            return list.Skip(1).All(x =>
    {
       Boolean b = y.CompareTo(x) < 0;
        y = x;
        return b;
    });
}

Upvotes: 1

Kevin Avignon
Kevin Avignon

Reputation: 2903

Knowing that you are working with a List data structure, you can call the sort method from the Collections API.

// Sorts the list of employees
Collections.sort(list);

Upvotes: 0

Andreas
Andreas

Reputation: 159086

Call this method:

public static <E extends Comparable<E>> boolean isSorted(Iterable<E> coll) {
    E prev = null;
    for (E value : coll) {
        if (prev != null && prev.compareTo(value) > 0)
            return false;
        prev = value;
    }
    return true;
}

Upvotes: 1

Related Questions