Java中Comparable和Comparator实现对象比较 - aheizi - 博客园
当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序。
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering. ------API
对字符串List可以直接sort进行排序, 那是因为String 这个对象已经帮我们实现了 Comparable接口 , 所以我们的 Person 如果想排序, 也要实现一个比较器。
Read full article from Java中Comparable和Comparator实现对象比较 - aheizi - 博客园
No comments:
Post a Comment