简体   繁体   中英

How to assert position of element on the list (Confirm it is first element on the list)

I am fairly new at this. I am trying assert that elements are positioned in descending order and first one is latest dated one(2019,2018,2017,2016 etc)

here are the elements with DATES:

}-->2017/2018

}-->2016/2017

}-->2015/2016

}-->2014/2015

Solution using com.google.common.collect is here :

boolean isSorted = Ordering.natural().reverse().isOrdered(actualDates);
Assert.assertTrue(isSorted);

You can use sort and assert using TestNG:

// Assume it's your actual dates to check
List<String> actualDates = new ArrayList<>();
actualDates.add("2014/2015");
actualDates.add("2016/2017");
actualDates.add("2017/2018");
actualDates.add("2015/2016");

// Create new list,  add all from actual list and order it. 
List<String> sortedDates = new ArrayList<>(actualDates);
sortedDates.sort(Comparator.reverseOrder());

Assert.assertEquals(actualDates, sortedDates);

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