Java 对多个列表进行排序
我有几张单子。例如,在Java 对多个列表进行排序,java,sorting,arraylist,Java,Sorting,Arraylist,我有几张单子。例如,在索引[1]中,我有一个列表[name,number1,number2],我想按number1对该列表排序,然后再按number2 这可能吗?[[姓名,20,10],[姓名,3,20],[姓名,5,40]] 我想换成:[姓名,20,10],[姓名,5,40],[姓名,3,20]] public class WriteToFile { Game game= new Game(); GUI gui= new GUI(); RandomAccessFile raf= null; S
索引[1]
中,我有一个列表[name,number1,number2]
,我想按number1
对该列表排序,然后再按number2
这可能吗?[[姓名,20,10],[姓名,3,20],[姓名,5,40]]
我想换成:[姓名,20,10],[姓名,5,40],[姓名,3,20]]
public class WriteToFile {
Game game= new Game();
GUI gui= new GUI();
RandomAccessFile raf= null;
Scanner scanner= new Scanner(System.in);
List gameInfoList= new ArrayList();
protected void fillList(){
gameInfoList.add(0,askName());
gameInfoList.add(1,game.flag);
gameInfoList.add(gui.different/1000);
try {
raf= new RandomAccessFile("D://Game.txt","rw");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
try {
raf.seek(raf.length());
raf.writeUTF(String.valueOf(gameInfoList));
raf.writeUTF("\r\n");
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {
raf.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
protected String askName() {
System.out.println(" Please Insert Your Name:");
String userName=scanner.next();
return userName;
}
}
public class MaxPoint implements Comparable {
int max;
public int compareTo(Object o) {
return 0;
}
WriteToFile writeToFile= new WriteToFile();
RandomAccessFile raf= null;
List lineList= new ArrayList();
Iterator it= lineList.iterator();
protected void findMax(){
try {
raf= new RandomAccessFile("D://Game.txt","r") ;
for(int i = 0;raf.readLine()!=null;i++){
lineList.add(i, raf.readLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}`
尝试:
List ll=/。。。
Collections.sort(ll,newcomparator(){
@凌驾
公共整数比较(列表l1、列表l2){
int n1=(整数)l1.get(1);
int n2=(整数)l2.get(1);
返回n2-n1;//降序
}
});
列表的类型是什么?[[string,int.int],…]按列表,是指数组吗?还是物品清单?请分享一些代码来阐明你想做什么。
List<List<Object>> ll = // ...
Collections.sort(ll, new Comparator<List<Object>>(){
@Override
public int compare(List<Object> l1, List<Object> l2) {
int n1 = (Integer) l1.get(1);
int n2 = (Integer) l2.get(1);
return n2 - n1; // descending order
}
});