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

时间:2019-03-18 21:24:42

标签: java

我无法验证日期数组是否已排序。

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”方法不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分: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;
}