简体   繁体   中英

How do verify ascending order of string values for an array list?

I am using Selenium Webdriver with Java binding and I'm testing a sorting functionality whereby you have values arranged in an arraylist = {"4"","4.5"","5.5""} . So basically the string contains decimal points as well as double quotation. I have the following code below. The problem is that I keep getting false due to the fact that when it compares the current to previous, it comes with false. Thanks for your help

public Boolean checkAscendingOrderScreensize(List<String> list){
   if(list == null || list.isEmpty())
        return false;
    if(list.size() == 1)  
        return true;
    for(int i=1; i<list.size();i++)
    {
        String current = list.get(i).toString();
        String previous = list.get(i-1).toString();
        current = current.replace(",",".");
        current = current.replace("\"", "");
        previous = previous.replace(",",".");
        previous = previous.replace("\"", "");

        if(current.compareTo(previous)>0)
                return false;
    }
    return true; 
}

You will need to covert the string to double before conversion. So the workaround will be

 public class Main {

    public static void main(String bicycle[]) {
        List<String> texts = new ArrayList<String>();
        texts.add("4\"");
        texts.add("4.5\"");
        texts.add("5.5\"");
        System.out.println(checkAscendingOrderScreensize(texts));
          // prints true

    }

    public static boolean checkAscendingOrderScreensize(List<String> list) {

        if (list == null || list.isEmpty())
            return false;

        if (list.size() == 1)
            return true;

        for (int i = 1; i < list.size(); i++) {
            String current = list.get(i).toString();
            String previous = list.get(i - 1).toString();
            current = current.replace(",", ".");
            current = current.replace("\"", "");
            previous = previous.replace(",", ".");
            previous = previous.replace("\"", "");

            if(Double.valueOf(current)<Double.valueOf(previous))


                return false;
        }

        return true;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM