有没有办法验证LinkedList <string>是否按降序排列?

时间:2018-04-02 10:56:01

标签: java string compareto

public static boolean verifyRecords(LinkedList<String> recordList, String order) throws Exception {

 /*To check ascending order*/
    if("Ascending".equalsIgnoreCase(order) || "A".equalsIgnoreCase(order)) {
        String previous = new String();
        for(String current : recordList) {
            if(current.compareTo(previous) < 0) 
                return false;
            previous = current;
        }
    } else if("Descending".equalsIgnoreCase(order) || "D".equalsIgnoreCase(order)) 
   {  
        for(String current : recordList) {
            if(current.compareTo(previous) > 0) 
                return false;
            previous = current;
        }
    } 
    return true;
}

在这里,我给新的String(),即Empty,字符串以Ascending顺序开头。但是对于降序,应该使用起始字符串来开始降序比较我应该使用什么?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

你不需要循环直到最后一个元素,你可以比较任何不平衡的东西,并从那里打破以实现你的结果或验证列表。

frontend web3_ssl_frontend
    bind <ipv4>:443
    bind <ipv6>:443
    mode tcp
    default_backend web3_ssl_backend

backend web3_ssl_backend
    balance roundrobin
    mode tcp
    cookie SERVERID insert indirect nocache
    default-server inter 4s rise 3 fall 2
    fullconn 20000
    reqadd X-Forwarded-Proto:\ https if { ssl_fc }

    option ssl-hello-chk
    server web1 192.168.163.2:443 maxconn 10000 check
    server web2 192.168.163.3:443 maxconn 10000 check

答案 1 :(得分:0)

您可以使用Collections.reverse(list)进行比较。我们的想法是创建recordList的浅表副本,按降序排序并比较它们。

    LinkedList<String> recordList = new LinkedList<String>(Arrays.asList("a","B", "c"));
    LinkedList<String> validateList = new LinkedList<String>(recordList);
    Collections.sort(validateList); // sorts in ascending order
    Collections.reverse(validateList); // reverses the ascending ordered list
    System.out.println(recordList.equals(validateList));