List<String[]> data = Importer.readCsv("test.csv");
A String[]
item looks like this:
2017-03-14 18:22:44.149
(String)What is a good approach in order to sort the list by the timestamp descending? I search for a solution without creating new Objects.
Use Collection.sort() and Comparator.comparing(), I find those easy to read when coming back to it later:
data.sort(Comparator.comparing((String[] o) -> o[1]).reversed());
You can compare the timestamp without conversion, as the format is "correctly" ordering the fields.
If you are confined to Java 7, there will be more boilerplate:
Collections.sort(data, new Comparator<String[]>()
{
@Override
public int compare(String[] o1, String[] o2)
{
return o2[1].compareTo(o1[1]);
// return -o1[1].compareTo(o2[1]);
}
});
Note that I need to compare o2
to o1
in this order or negate the result of comparing o1
to o2
to order the data descending. This becomes much more obvious in the first approach.