第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

List.sort 比較器排序

List.sort 比較器排序

查詢到的數(shù)據(jù)放入List中,T為實體類,假設(shè)有兩個字段A和B當(dāng) A==1時將其排序靠前,并按照B字段排序也就說,A==1的數(shù)據(jù),要在A!=1的數(shù)據(jù)前面,A==1的數(shù)據(jù)還要再根據(jù)B字段排序,請不吝賜教。
查看完整描述

1 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Untitled {
    public static void main(String[] args) {
        List<I> list = new ArrayList<I>();
        list.add(new I(1, 3));
        list.add(new I(1, 2));
        list.add(new I(1, 5));
        list.add(new I(0, 5));
        list.add(new I(7, 1));
        list.add(new I(8, 0));
        list.add(new I(9, 3));
        list.add(new I(4, 9));
        list.add(new I(5, 5));
        Collections.sort(list, new Comparator<I>() {
            @Override
            public int compare(I o1, I o2) {
                if (o1.A == 1 && o2.A == 1)
                    return o1.B - o2.B;
                if (o1.A == 1 || o2.A == 1)
                    return o1.A == 1 ? -1 : 1;
                return o1.B - o2.B;
            }
        });
        System.out.println(list);
    }
}

class I
{
    public int A;
    public int B;
    public I(int a, int b) { A = a; B = b; }
    @Override
    public String toString() {
        return A + "," + B;
    }
}

運行結(jié)果
[1,2, 1,3, 1,5, 8,0, 7,1, 9,3, 0,5, 5,5, 4,9]

查看完整回答
反對 回復(fù) 2018-07-31
  • 1 回答
  • 0 關(guān)注
  • 1039 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號