这个数据保存在data.txt中我正在尝试编写一个可以安排的程序
18b0885 // this is the registration number, bullet points to show indentation
SS844 Parallel Algorithms // These are course taken by student
SS555 Calculus for Distributed Computing
SS501 Quantum Communication
17b0585
SS828 Problem Based Programming
SS660 Genetic Computation
SS567 Hacking Quantum Network
17b2582
SS567 Hacking Quantum Network
SS876 Positronics
SS880 Quark-based Logic
像这样的一大串数据,需要编写一个程序来缩短这些数据,按注册号升序排列,课程将跟随注册号。所以预期的输出是这样的。
17b2582
SS567 Hacking Quantum Network
SS876 Positronics
SS880 Quark-based Logic
17b0585
SS828 Problem Based Programming
SS660 Genetic Computation
SS567 Hacking Quantum Network
18b0885
SS844 Parallel Algorithms
SS555 Calculus for Distributed Computing
SS501 Quantum Communication
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Sort {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
Map<String, List<String>> map = new TreeMap<String, List<String>>();
String line = reader.readLine();//read header
while ((line = reader.readLine()) != null) {
String key = getField(line);
List<String> l = map.get(key);
if (l == null) {
l = new LinkedList<String>();
map.put(key, l);
}
l.add(line);
}
reader.close();
FileWriter writer = new FileWriter("sorted_numbers3.txt");
writer.write("");
for (List<String> list : map.values()) {
for (String val : list) {
writer.write(val);
writer.write("\n");
}
}
writer.close();
}
private static String getField(String line) {
return line.split(",")[0];//
}
}
上面的程序输出到另一个像这样的文本文件中
SS501 Quantum Communication
SS555 Calculus for Distributed Computing
SS567 Hacking Quantum Network
SS567 Hacking Quantum Network
SS660 Genetic Computation
SS828 Problem Based Programming
SS844 Parallel Algorithms
SS876 Positronics
SS880 Quark-based Logic
17b2582
17b0585
18b0885
它通过升序然后注册号来缩短所有课程,但不是我想要的。我应该改变什么?