所以我这里有这个代码,需要一个文件,并将其放入一个数组。我现在需要做的是将第二列中的整数从大到小排序。这里是我的代码,并在底部有一个指向数据文件的链接。我知道有排序算法,但我不知道如何实现它们。整数数组排序
import java.util.*;
import java.io.*;
public class sorter{
public static int id = 0;
public static int score = 0;
public static void main(String args[]){
Scanner inFile = null;
try {
inFile = new Scanner (new File ("sorter.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
while (inFile.hasNextLine()){
String str = inFile.nextLine();
String [] parts = str.split(" ");
String part1 = parts[0];
String part2 = parts[1];
id = Integer.parseInt(part1);
score = Integer.parseInt(part2);
System.out.println(part1 + " " +part2);
}
}
}
这里是输出应该是什么:
/*
ID Score
305 265
306 262
115 257
311 256
123 253
116 246
325 246
321 245
323 245
113 243
218 243
208 242
302 242
112 239
104 239
110 238
223 230
213 229
207 228
203 224
222 223
*/
那么通常情况下,你不不必执行它们。我会看看Java Arrays API。我相信你可以用一种排序数组的方式来。试着在纸上画出你将如何排序一个小阵列 –
@ user3259415你是按ID还是Score排序? – Mozzie
那么,你应该先救每一个得分/ ID,排序它然后通过分数打印 –