Я написал код для сортировки многомерного ArrayList ArrayList<String[]>
по 1-му элементу каждого String[]
, 2dArray выглядит так:
{
["Mark", "mark@mail.com"],
["Angela", "zzAngela@gmail.com"],
["Harry", "abc_harry@hotmail.com"],
["Donald","donny@aol.com"]
}
Но когда я запускаю код, массив не 'кто-то может объяснить, что не так с моим кодом?Спасибо уже!
ArrayList<String[]> sorter = new ArrayList<String[]>();
String file_name = "db_email.csv";
// READING FROM CSV FILE & STORING DATA INTO 2dARRAY
try{
BufferedReader reader = new BufferedReader(new FileReader (file_name));
String line = reader.readLine(); // read the rest
String[] row;
while(line != null){
String[] result = line.split(",");
sorter.add(new String[]{result[0], result[1]});
line = reader.readLine();
}
reader.close();
} catch (IOException e){
System.out.println(e);
}
// MY SORTING ALGORITHM
Collections.sort(sorter, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareTo(o2[0]);
}
});
// EXPECTED RESULT (sorting 2dArray by 1st column):
{
["Angela", "zzAngela@gmail.com"],
["Donald","donny@aol.com"],
["Harry", "abc_harry@hotmail.com"],
["Mark", "mark@mail.com"]
}
// ACTUAL RESULT (the array doesn't get sorted):
{
["Mark", "mark@mail.com"],
["Angela", "zzAngela@gmail.com"],
["Harry", "abc_harry@hotmail.com"],
["Donald","donny@aol.com"]
}