如何检查给定的字符串或日期数组是否已排序。附上我的代码

问题描述 投票:0回答:1

我不能验证日期数组是否排序。

public void FilterBy() throws IOException, InterruptedException, ParseException {
    Login();
        ConvPage cmp = new ConvPage(driver);
            cmp.clickDateSorting();
            int ActualConvNum = cmp.ConvTable().findElements(By.xpath(cmp.ConvPath())).size();
            Date[] versions = new Date[ActualConvNum-700];
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
            for(int i = 0; i<ActualConvNum-700;i++) {
                String date = driver.findElement(By.xpath(cmp.XpathForVersionCell(i+1))).getText();
                versions[i] = sdf.parse(date);
            }

                        if (ArraySortVal(versions)) { 
                System.out.println("Same"); }
            else {
                System.out.println("Not same"); 
            }


    }

    public static  boolean ArraySortVal(Date[] arr) {
        List<Date> copyOf = new ArrayList<>(Arrays.asList(arr));
        Collections.sort(copyOf);
            if (Arrays.asList(arr).equals(copyOf)){
            return true;
            } else {
           return false; // Not sorted of course but if Month
            }

    }

我正在获取一个日期数组,并希望确定它是否已排序。 “ArraySortVal”方法不起作用。有任何想法吗?

java
1个回答
0
投票

您可以使用before类中定义的java.util.Date方法:

public boolean isSorted(Date[] dates) {
 if(dates.length <= 1) return true;
 for(int i = 1; i < dates.length; ++i) {
  // you can check with > operator if you want to test the reverse order
  if(dates[i].before(dates[i - 1])) return false;
 }
 return true;
}

© www.soinside.com 2019 - 2024. All rights reserved.