所以我這裏有這個代碼,需要一個文件,並將其放入一個數組。我現在需要做的是將第二列中的整數從大到小排序。這裏是我的代碼,並在底部有一個指向數據文件的鏈接。我知道有排序算法,但我不知道如何實現它們。整數數組排序
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,排序它然後通過分數打印 –