假设你能得到一个读者
StringReader in = new StringReader(
"Patrick, Gary, Male, Blue, 1/28/1948\n" +
"Carson, Larry, Male, Pink, 11/24/1976\n" +
"Fisher, Paul, Male, Orange, 5/12/1995");
您可以使用缓冲阅读器逐行阅读并使用列表来累积行
List<String[]> splitLines = new ArrayList<String[]>();
try {
BufferedReader lineIn = new BufferedReader(in);
for (String line; (line = lineIn.readLine()) != null;) {
String[] lineParts = line.split(",");
// Maybe check that lineParts has the right length here.
splitLines.add(lineParts);
}
} finally {
in.close();
}
然后您可以将列表转换为二维数组。
String[][] linesArray = splitLines.toArray(new String[][] {});
然后您可以使用自定义比较器对其进行排序。
Array.sort(linesArray, new Comparator<String[]>() {
public int compare(String[] linea, String[] lineb) {
... // the date should be in linea[4] and lineb[4].
}
})();