我列出了几个清单。例如在index[1]
我有一个列表[name, number1, number2]
,我希望按number1
排序此列表,之后number2
这可能吗? [[姓名,20,10],[姓名,3,20],[姓名,5,40]]
我想改变:[[name,20,10],[name,5,40],[name,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();
}`
答案 0 :(得分:1)
尝试:
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
}
});