I have a complex "Order" object parsed from json. Somewhere deep in the structure i have the orderDate. I can extract it only as a STRING! And now I try to sort a List of Orders in place.
I came with the Idea of creating a new List, where the array inside consist two elements, first the Order Object itself, second the Date parsed to A Date object. eg. new Object[]{order, new Date(order.getOrderDate())}. Then sort by second element and then parse back to a List and return. But this creates two new Lists and is not in place.
The other idea is to create a custom Comparator that sorts like this
orders.sort(new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return new Date(o1.getOrderDate()).compareTo(new Date(o2.getOrderDate()));
}
});
But the second variant will create a lot of new Date Objects. Worst case a lot times for every entry.
Is there a more beautiful way around it?