Store the information in memory using a class that implements the Collection
interface.  A Collection is simply a class that stores lists of things.
ArrayList is an example of a class that allows you to store as many objects
of any type as you want.  It implements the Collection interface.

Then retrieve or create an instance of a class that implements the
Comparator interface.  A comparator class is a class that can compare two of
the objects you have stored in your collection.  If you are just storing
Strings then use the static getInstance() method of the Collator class to
retrieve a comparator.  It will give you a comparator object that knows how
to sort Strings in a locale, (or geographic location), specific way.  If you
are storing custom objects then you may need to write your own class to do
the comparison and have it implement the Comparator interface.

You can then sort the collection using a single call to Collections.sort().
This is a static method that takes a collection and a comparator as
parameters.  When the call finishes, the collection is sorted.

Here is an example of some code that stores a list of Strings in an
ArrayList and then sorts them using locale specific String comparisons.

import java.util.*;
import java.text.*;

public class sorttest
{
        static public void main( String[] args )
        {
                //Instantiate the collection class that will store the Strings.
                ArrayList myCollection = new ArrayList();

                //Put the Strings in the collection.
                myCollection.add("Phelps, Mark");
                myCollection.add("Jones, Fred");
                myCollection.add("Adams, Sally");
                myCollection.add("Gonzales, Jose");

                //Get a Collator object that is specific to this JVMs default locale.
                Collator myComparator = Collator.getInstance();

                //Call a static method in the Collections class to sort the collection.
                Collections.sort( myCollection, myComparator );

                //Print out the sorted Strings
                for( int loop = 0; loop < myCollection.size(); loop++ )
                {
                        System.out.println( (String)myCollection.get( loop ) );
                }
        }
}

-----Original Message-----
From: JustinMacCarthy [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 29, 2001 4:09 AM
To: JRun-Talk
Subject: Java Sorting


What the best ways to sort somthing like these (eg by firstname , surname
,position) but for 20000 rows ?.

Assuming worst case ordering

String[][] data =

            { "Stephen", "Cheng", "Vice President" },
            { "Joe", "Berrey", "Intern" },
            { "Adam", "Lipinski", "Director" },
            { "Lynne", "Teague", "Developer" }
            {..... etc }

}

Can do it Perl etc , but Im a Java  newbie.
Thanks

J
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to