#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    1
    Rep Power
    0

    Wink sorting of data from a file


    Hi

    I have an external text file with some data. I manage to extract the data out and each piece of data consist of many fields. Before displaying the data on a JSP page, I need to sort all the data by a particular field.
    What I have done is actually put all the data into a 2D array and sort them.

    But because there are too much data, the processing and computation take very long.
    Does anybody have a better idea or solution to go around this?

    Thanks....
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Use a database? Maybe you could provide a text file that is pre-sorted? This doesn't sound like something you're going to be wanting to do every time you view the page.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0
    Since you're in java, another thing you can do is have a servlet that loads the text file, sorts it and keeps it in an object that your other page uses. Depending on how the data is updated the serlvet could reload the text file every hour, or check time of last change or whatever. Since servlets stay in memory they can have data that lasts between requests.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    1
    Rep Power
    0
    hhmmm... what I do is the following:

    1o.) I have a vector of objects (these objects are javaBeans). Every object has many fields (price,name, you name it).

    2o.) now, you can put this vector in a session (in my case the vector is not too big, so I don't have performance issues).

    3o. ) now, my javabeans implement the comparable interface, something like this:

    public class Employee implements Comparable { public static final Comparator SOC_SEC_ORDER = new Comparator( ) { public int compare(Object o1, Object o2) { Employee emp1 = (Employee)o1; Employee emp2 = (Employee)o2; return emp1.getSocSecNum( ).compareTo(emp2. getSocSecNum( )Here; } }; public static final Comparator NAME_ORDER = new Comparator( ) { public int compare(Object o1, Object o2) { Employee emp1 = (Employee)o1; Employee emp2 = (Employee)o2; return emp1.getName( ).compareTo(emp2. getName( ) Here; } };}

    4o.) now, the only thing you do to order is.

    Collections.sort(myVector,SOC_SEC_ORDER)

    and that's it. Remember, your beans must be serializable so you don't have memory problems..

    hope it helps
    andres
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    1
    Rep Power
    0
    ooppss.. screwed up my code. sorry.

IMN logo majestic logo threadwatch logo seochat tools logo