如何验证列表中的元素是否按字母顺序排序

时间:2015-10-12 21:36:16

标签: java c#

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   
  }

这里我需要验证员工是按字母顺序排序还是不是boolean == true;如果员工按字母顺序排列

3 个答案:

答案 0 :(得分:1)

调用此方法:

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;
}

答案 1 :(得分:1)

以下代码适用于我的排序验证

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;
    });
}

答案 2 :(得分:0)

知道您正在使用List数据结构,您可以从Collections API调用sort方法。

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