sorting

R

Rookie

What is the most efficent way to sort a LARGE list?

The data is read in from file and the file get up dated daily and not sorted
and during the day more data is addded. The data need to be sort and
increase the speed of retiving data using a binary search.
 
H

Hoss Spence

I've always found the best way to sort a list is to use
Collections.sort (List, Comparator).

If the objects in the List implement Comparable then you can, in your
Comparator, cast your
objects to Comparable and use the compareTo method. This is
particularly useful
if don't know what your Object is other than it's one of the standards
e.g. Long, Integer, Date which implement Comparable.
 
J

John C. Bollinger

Hoss said:
I've always found the best way to sort a list is to use
Collections.sort (List, Comparator).

If the objects in the List implement Comparable then you can, in your
Comparator, cast your
objects to Comparable and use the compareTo method. This is
particularly useful
if don't know what your Object is other than it's one of the standards
e.g. Long, Integer, Date which implement Comparable.

If the objects in the List are all mutually Comparable then you can use
Collections.sort(List) instead. That has the advantages you mentioned,
plus you don't need to write or use a Comparator (no matter how trivial).


John Bollinger
(e-mail address removed)
 
H

Hoss Spence

John C. Bollinger said:
If the objects in the List are all mutually Comparable then you can use
Collections.sort(List) instead. That has the advantages you mentioned,
plus you don't need to write or use a Comparator (no matter how trivial).


John Bollinger
(e-mail address removed)
While that's true in the simple case, in many cases the Object in the
List is a form bean consisting of only Strings yet semantically the
sort is let's say a Date or a Numeric. In that case you need to
specifiy the Comparator because you have to convert the String (via
reflection on the getter) to the type of Object you are comparing, and
then cast it to a Comparable. Of course to do the conversion to the
appropriate object the Comparator needs to know the type which can be
passed in a Wrapper Method which contains the Comparator. But the
beauty is that one Comparator can do all this due to the Comparable
cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top