简体   繁体   English

如何声明元素在列表中的位置(确认它是列表中的第一个元素)

[英]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) 我正在尝试断言元素以降序排列并且第一个是最新的日期(2019,2018,2017,2016等)

here are the elements with DATES: 以下是DATES的元素:

}-->2017/2018 }-> 2017/2018

}-->2016/2017 }-> 2016/2017

}-->2015/2016 }-> 2015/2016

}-->2014/2015 }-> 2014/2015

Solution using com.google.common.collect is here : 使用com.google.common.collect解决方案在这里

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

You can use sort and assert using TestNG: 您可以使用TestNG使用sort和assert:

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按日期排序列表,然后在第一个位置搜索元素和设置 - How to order a list by date and then search a element and setting at first position 对元素仍处于第一个位置的列表进行排序 - Sort a list with element still in first position 使用 Stream 将列表中的元素移动到第一个 position - Move an element in the list to the first position using Stream 更改元素在列表中的位置 - Change the position of element in List 将 Map 的第一个元素添加到第一个 Position 的另一个列表中,依此类推...第二个 map 元素到第二个 Z4757FE07FD492ADBEEAZ6 列表中 - Add first element of Map into another list at 1st Position and so on…second map element into second position of list 如何将List中的位置绑定到该元素的属性? - How to bind the position in a List to an attribute of that element? 在第一个 position 插入元素时返回不正确的链接列表 - Incorrect Linked List being returned when inserting element at first position 如何排序列表<List<String> &gt; 按每个列表的第一个元素? - How to sort List<List<String>> by first element of every list? 如何删除链表中的第一个元素? - How to delete the first element in a linked list? 如何从链接列表中删除第一个元素? - How to remove first element from the Linked List?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM