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>() {
            @Override
            public 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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章