方法一:根据java1.8lambda表达式进行排序

Comparator<RateInfo> comparator = (t1, t2) -> t1.getRateCode().compareTo(t2.getRateCode());

方法二:使用List的方法sort()排序

List API:default void sort(Comparator<? super E> c)

其实也是依据Comarator这个类

rateInfolist.sort(comparator.reversed());

方法三:使用Collections类的sort进行排序

static <T> void sort(List<T> list, Comparator<? super T> c)

Sorts the specified list according to the order induced by the specified comparator.
谷歌翻译:根据指定比较器引发的顺序对指定列表进行排序。
其实该排序也是使用Comparator类进行排序
Comparator类API:英文翻译即可
int     compare(T o1, T o2)
Compares its two arguments for order.
static <T,U extends Comparable<? super U>>
Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
Accepts a function that extracts a sort key from a type T, and returns a Comparator<T> that compares by that sort key using the specified Comparator.
static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
Accepts a function that extracts a double sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
Accepts a function that extracts an int sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
Accepts a function that extracts a long sort key from a type T, and returns a Comparator<T> that compares by that sort key.
boolean equals(Object obj)
Indicates whether some other object is "equal to" this comparator.
static <T extends Comparable<? super T>>
Comparator<T> naturalOrder()
Returns a comparator that compares Comparable objects in natural order.
static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers null to be less than non-null.
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers null to be greater than non-null.
default Comparator<T> reversed()
Returns a comparator that imposes the reverse ordering of this comparator.
static <T extends Comparable<? super T>>
Comparator<T> reverseOrder()
Returns a comparator that imposes the reverse of the natural ordering.
default Comparator<T> thenComparing(Comparator<? super T> other)
Returns a lexicographic-order comparator with another comparator.
default <U extends Comparable<? super U>>
Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a Comparable sort key.
default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator.
default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a double sort key.
default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a int sort key.
default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a long sort key.

代码:

Collections.sort(rateInfolist, Comparator.comparing(RateInfo::getRateCode));

方法四:使用Comparator的匿名对象类重写compare方法

代码:

Collections.sort(rateInfolist, new Comparator<RateInfo>(){
/*
* int compare(RateInfo R1, RateInfo R2) 返回一个基本类型的整型,
* 返回负数表示:R1 小于R2,
* 返回0 表示:R1和R2相等,
* 返回正数表示:R1大于R2
*/
public int compare(RateInfo R1, RateInfo R2) {
Integer rateCode1 = Integer.parseInt(R1.getRateCode());
Integer rateCode2 = Integer.parseInt(R2.getRateCode());
//按照RateCode的年龄进行升序排列
if(rateCode1 > rateCode2){
return 1;
}
if(rateCode1 == rateCode2){
return 0;
}
return -1;
}
});

自己写代码时遇到的问题,根据我的理解和网上的资料做的总结

最新文章

  1. XAF学习笔记之-多表头设计
  2. numpy使用
  3. 2016CCPC东北地区大学生程序设计竞赛 1003 HDU5924
  4. hdu 4764 Stone (巴什博弈,披着狼皮的羊,小样,以为换了身皮就不认识啦)
  5. c#接口相关
  6. 【Vijos】【1164】曹冲养猪
  7. java学习笔记_GUI(4)
  8. js创建对象的几种常用方式小结
  9. SDL2源码分析5:更新纹理(SDL_UpdateTexture())
  10. Linux - 设置/取消代理
  11. 漂亮CSS样式用户留言表单
  12. Java引入的一些新特性
  13. mybatis原理分析学习记录,mybatis动态sql学习记录
  14. 使用MagicAJax的AjaxPanel时有时会弹出&quot;Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed.&quot;
  15. NSUserDefaults 添加与删除
  16. FAILED Selenium2Library
  17. 【RF库Collections测试】Get Dictionary Items
  18. echarta3 北京,上海地图
  19. LINUX中软RAID的实现方案
  20. Hbase的安装和配置

热门文章

  1. 第六篇:fastJson常用方法总结
  2. element表单验证
  3. Spring - 整合MyBatis
  4. WPF 字体描边的实现方式
  5. 编写Map处理逻辑
  6. 2016.9.10初中部上午NOIP普及组比赛总结
  7. 完全卸载之前8.0的Mysql,安装5.5mysql
  8. 容斥原理——hdu2204dfs深搜
  9. vue cnpm run dev 报错,解决方法
  10. 如何用maven读取本地架包