如何按字母顺序和姓氏按名字排序

时间:2017-09-29 15:37:22

标签: java appium

我有3个元素,它们的名字和姓氏是一个按钮。我需要检查它们是按名字排序,然后按字母顺序按姓氏排序。元素的页面对象是ListV_Sorted。我想我只是按名字元素排序。 0元素= Jack Daniels,1元素= John Fera,2元素= Mike Durov。 ListV是元素的页面对象。我得到一个错误它说预期为真但发现错误。

@SuppressWarnings("unchecked")
public void sortingOrder() {

    Log.log(driver).info("About to Alphebetically Sort");
    List<MobileElement> products_MobileElements = new LinkedList<MobileElement>();
    products_MobileElements = (List<MobileElement>) TicketPassesNames;
    LinkedList<String> product_names =  new LinkedList<String>();

    for(int i=0;i<products_MobileElements.size();i++){

        String s = products_MobileElements.get(i).getAttribute("checked");
        String[] tokens = s.split("");
        String firstName = "";
        String lastName = "";
        if(tokens.length > 0) {
            firstName = tokens[0];
            lastName = tokens[tokens.length -1];
            product_names.add(s);
            product_names.add(firstName);
            product_names.add(lastName);
        }
    }
    boolean result = checkAlphabeticalOrder(product_names);

    Assert.assertEquals(checkAlphabeticalOrder(product_names), true);
    Log.log(driver).info("Tickest Passes names are in alphabetical order.");
    System.out.println(result);
}

//Method takes a String to Sort AlphabeticalLy
public static boolean checkAlphabeticalOrder(LinkedList<String> product_names){

    String previous = ""; // empty string

    for (final String current: product_names) {
        if (current.compareTo(previous) < 0)
            return false;
        previous = current;
    }

    return true;
}

1 个答案:

答案 0 :(得分:1)

您应该使用java.util.LinkedList来使用数据结构对输入进行排序,而不是java.util.TreeSet

我创建了一个稍微更改过的代码版本,在程序执行后打印“true”。但主要区别在于使用java.util.TreeSetCollection接口。

以下是更改后的版本:

import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class SimpleTests2 {

    public static void main(String[] args) throws ParseException {

        sortingOrder();
    }

    //Method takes a String to Sort AlphabeticalLy
    public static boolean checkAlphabeticalOrder(Collection<String> product_names) {

        String previous = ""; // empty string

        for (final String current : product_names) {
            if (current.compareTo(previous) < 0)
                return false;
            previous = current;
        }

        return true;
    }

    @SuppressWarnings("unchecked")
    public static void sortingOrder() {

//        Log.log(driver).info("About to Alphebetically Sort");
        List<MobileElement> products_MobileElements;
        products_MobileElements = Arrays.asList(
                new MobileElement("Mike Durov"), new MobileElement("Jack Daniels"), new MobileElement("John Fera"));
        // Here is the main change to your code!
        Collection<String> product_names = new TreeSet<>();

        for (int i = 0; i < products_MobileElements.size(); i++) {

            String s = products_MobileElements.get(i).getAttribute("checked");
            String[] tokens = s.split(" ");
            String firstName = "";
            String lastName = "";
            if (tokens.length > 0) {
                firstName = tokens[0];
                lastName = tokens[tokens.length - 1];
                product_names.add(s);
                product_names.add(firstName);
                product_names.add(lastName);
            }
        }
        boolean result = checkAlphabeticalOrder(product_names);

//        Assert.assertEquals(checkAlphabeticalOrder(product_names), true);
//        Log.log(driver).info("Tickest Passes names are in alphabetical order.");
        System.out.println(result);
        System.out.println(product_names);
    }


    private static class MobileElement {

        private String name;

        public MobileElement(String name) {
            this.name = name;
        }

        public String getAttribute(String checked) {
            return name;
        }
    }
}