當前位置:編程學習大全網 - 網站源碼 - Collections.sort方法對數據庫取出的list排序 按著5個key 進行排序,求解!~java

Collections.sort方法對數據庫取出的list排序 按著5個key 進行排序,求解!~java

看例子

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

List<Record> list = new ArrayList<Record>();

Collections.sort(list, new Comparator<Record>(){

public int compare(Record o1, Record o2) {

if(o1.getKey1() > o2.getKey1()){

return 1;

}else if(o1.getKey1() == o2.getKey1()){

if(o1.getKey2() > o2.getKey2()){

return 1;

}else if(o1.getKey2() == o2.getKey2()){

//TODO: compare key3

return -1;

}else{

return 0;

}

}else{

return 0;

}

}

});

}

}

class Record{

private int key1, key2, key3;

public int getKey1() {

return key1;

}

public int getKey2() {

return key2;

}

public int getKey3() {

return key3;

}

}

  • 上一篇:永久出特規律公式
  • 下一篇:videojs在chrome中無法正常顯示,火狐和ie11可以,怎麽回事
  • copyright 2024編程學習大全網