list的排序Comparator的compare(T lhs, T rhs)

编程入门 行业动态 更新时间:2024-10-21 13:41:19

list的排序<a href=https://www.elefans.com/category/jswz/34/1763323.html style=Comparator的compare(T lhs, T rhs)"/>

list的排序Comparator的compare(T lhs, T rhs)

归纳总结

public int compare(T lhs, T rhs)
返回值有1,0,-1
1. 升序排列,如何通过返回值控制?
当lhs.property>rhs.property返回1;lhs.property==rhs.property返回0;lhs.property<rhs.property返回-1。
2. 降序排列,如何通过返回值控制?
当lhs.property>rhs.property返回-1;lhs.property==rhs.property返回0;lhs.property<rhs.property返回1。
3. 组合排列,如何通过返回值控制?
以本测试代码为例,默认选中(defaultFlag==1)的地址第一个显示,剩下的按id升序排列
第一层判断defaultFlag
当lhs.defaultFlag==rhs.defaultFlag,进行第二层判断,判断id
注:lhs.property指一个字符的unicode值

测试代码

/*** @author :renpan* @version :v1.0* @class :com.luomo.addressselected* @date :2016-08-15 11:41* @description:*/
public class Test {public static void main(String[] args) {List<AddressDomain> addresses = new ArrayList<AddressDomain>();addresses.add(new AddressDomain("1", "曹阿瞒", "18710990897", "0"));addresses.add(new AddressDomain("2", "关羽", "18710990896", "0"));addresses.add(new AddressDomain("3", "刘备", "18710990895", "0"));addresses.add(new AddressDomain("4", "司马懿", "18710990894", "1"));addresses.add(new AddressDomain("5", "张飞", "18710990893", "0"));addresses.add(new AddressDomain("6", "诸葛亮", "18710990892", "0"));Collections.sort(addresses, new Comparator<AddressDomain>() {@Overridepublic int compare(AddressDomain lhs, AddressDomain rhs) {//CODE_1:升序排列/*if(Integer.parseInt(lhs.getId())>Integer.parseInt(rhs.getId())){return 1;}else if(Integer.parseInt(lhs.getId())==Integer.parseInt(rhs.getId())){return 0;}else{return -1;}*///CODE_2:降序排列/*if(Integer.parseInt(lhs.getId())>Integer.parseInt(rhs.getId())){return -1;}else if(Integer.parseInt(lhs.getId())==Integer.parseInt(rhs.getId())){return 0;}else{return 1;}*///CODE_3:组合排列if (Integer.parseInt(lhs.getDefaultFlag()) > Integer.parseInt(rhs.getDefaultFlag())) {return -1;} else if (Integer.parseInt(lhs.getDefaultFlag()) == Integer.parseInt(rhs.getDefaultFlag())) {if (Integer.parseInt(lhs.getId()) > Integer.parseInt(rhs.getId())) {return 1;} else if (Integer.parseInt(lhs.getId()) == Integer.parseInt(rhs.getId())) {return 0;} else {return -1;}} else {return 1;}}});for (int i = 0; i < addresses.size(); i++) {System.out.println(addresses.get(i).toString());}}
}

结果

CODE_1:升序排列 id

AddressDomain{id='1', name='曹阿瞒', mobilePhone='18710990897', defaultFlag='0'}
AddressDomain{id='2', name='关羽', mobilePhone='18710990896', defaultFlag='0'}
AddressDomain{id='3', name='刘备', mobilePhone='18710990895', defaultFlag='0'}
AddressDomain{id='4', name='司马懿', mobilePhone='18710990894', defaultFlag='1'}
AddressDomain{id='5', name='张飞', mobilePhone='18710990893', defaultFlag='0'}
AddressDomain{id='6', name='诸葛亮', mobilePhone='18710990892', defaultFlag='0'}

CODE_2:降序排列 id

AddressDomain{id='6', name='诸葛亮', mobilePhone='18710990892', defaultFlag='0'}
AddressDomain{id='5', name='张飞', mobilePhone='18710990893', defaultFlag='0'}
AddressDomain{id='4', name='司马懿', mobilePhone='18710990894', defaultFlag='1'}
AddressDomain{id='3', name='刘备', mobilePhone='18710990895', defaultFlag='0'}
AddressDomain{id='2', name='关羽', mobilePhone='18710990896', defaultFlag='0'}
AddressDomain{id='1', name='曹阿瞒', mobilePhone='18710990897', defaultFlag='0'}

CODE_3:组合排列 defaultFlag>id(如果要根据姓名排序,要先获取汉字的拼音,再比较)

AddressDomain{id='4', name='司马懿', mobilePhone='18710990894', defaultFlag='1'}
AddressDomain{id='1', name='曹阿瞒', mobilePhone='18710990897', defaultFlag='0'}
AddressDomain{id='2', name='关羽', mobilePhone='18710990896', defaultFlag='0'}
AddressDomain{id='3', name='刘备', mobilePhone='18710990895', defaultFlag='0'}
AddressDomain{id='5', name='张飞', mobilePhone='18710990893', defaultFlag='0'}
AddressDomain{id='6', name='诸葛亮', mobilePhone='18710990892', defaultFlag='0'}

源码释义

    /*** Compares the two specified objects to determine their relative ordering. * 比较两个指定的对象以决定它们的相对顺序* The ordering implied by the return value of this method for all possible pairs of* {@code (lhs, rhs)} should form an <i>equivalence relation</i>.* 这个顺序排列是依赖于compare函数的返回值* This means that* <ul>* <li>{@code compare(a,a)} returns zero for all {@code a}</li>* compare(a,a)返回0* <li>the sign of {@code compare(a,b)} must be the opposite of the sign of {@code* compare(b,a)} for all pairs of (a,b)</li>* compare(a,b)与compare(b,a)返回值是相反的* <li>From {@code compare(a,b) > 0} and {@code compare(b,c) > 0} it must* follow {@code compare(a,c) > 0} for all possible combinations of {@code* (a,b,c)}</li>* </ul>* compare(a,b) > 0、compare(b,c) > 0则compare(a,c) > 0** @param lhs*            an {@code Object}.* @param rhs*            a second {@code Object} to compare with {@code lhs}.* @return an integer < 0 if {@code lhs} is less than {@code rhs}, 0 if they are*         equal, and > 0 if {@code lhs} is greater than {@code rhs}.* @throws ClassCastException*                if objects are not of the correct type.*/public int compare(T lhs, T rhs);

更多推荐

list的排序Comparator的compare(T lhs, T rhs)

本文发布于:2024-02-06 02:42:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Comparator   list   compare   rhs   lhs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!