Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Comparator for multiple column sorting

User_19BPUMar 10 2012 — edited Mar 14 2012
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2012
Added on Mar 10 2012
9 comments
509 views