Comparator for multiple column sorting
Hi,
Wr have written a below query
select p.name, o.id, ol.name, pr.name from Person p , Order o , OrderLine ol, Product pr where...
order by p.key asc, o.key asc, ol.key desc, pr.key desc
This query is taking lot of time as it seems the ORDER BY is taking more time. Hence we thought of moving sorting in the java side.
Here whether I need to create 4 comparators
1) PersonComparator with p.key asc
2) OrderComparator with o.key asc
3) OrderLineComparator with ol.key desc
4) ProductComparator with pr.key desc
This query returns a list , hence we need to do some thing like this
Collections.sort(list, new PersonComparator())
Collections.sort(list, new OrderComparator())
Collections.sort(list, new OrderLineComparator())
Collections.sort(list, new ProductComparator())
Is there any other better way of handling this sorting as 2 fields needs asc and other 2 fields needs desc. Whether we need to create 4 comparators for each object?
Please clarify. If there is any alternative please specify that as well.
Thanks.