Need a help with search results sorting solution


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Need a help with search results sorting solution

  1. #1
    Join Date
    Jul 2005
    Posts
    2

    Need a help with search results sorting solution

    I can not find an efficient solution for sorting search results from the database. My search results returns members of our service, including personal information about each member. Search can produce hundreds of results, and information about each member might contain his/her profile and essays, so I have to be careful about the memory.

    The search results have to be sortable by member's age, activity , registration date and so on. I was thinking about a linked list of object (where each object represent a member). To sort this list I have to iterate through the list, extract the sorting parameter , sort them, and rebuild search results list in a new order.

    I thought that may be someone had a similar task before, and knows an efficient way how to do that. I would appreciate any ideas. May be not only about sorting, but about cashing this search results as well.

    Thanks
    Agady

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Check out the Collections.sort(List list, Comparator cmp) method. If you
    store the search results as separate object (i.e. records) in an ArrayList you
    can implement the java.util.Comparator interface in some suitable class.
    There you implement the compare and equals methods.
    The public boolean equals method can just as well return false as it has no
    effect on sorting.

    The public int compare(Object ob1, Object ob2) must return a positive
    value if ob1 > ob2, 0 if ob1==ob2 and a negative value if ob1 < ob2.

    It can be done like this:

    Store the records in an ArrayList of objects (SrcArrayList), say, RecordObj
    that defines the fields of one record of retrieved data.

    Make a separate class, say, SearchSorter that implements Comparator

    Code the equals method like I said, and then code the compare method like this. This example sorts on ascending age, if age is equal it sorts on name:

    Code:
    public int compare(Object ob1, Object ob2) {
      RecordObj rO1=(RecordObj)ob1;
      RecordObj rO2=(RecordObj)ob2;
      if (sStyle==SearchSorter.BY_AGE_AND_NAME) {
        if (rO1.getAge() != rO2.getAge()) {
          return rO1.getAge()-rO2.getAge();
        }
        // age is the same, let name decide, if name is the same
        // then the sort sequence of rO1 and rO2 will be arbitrary.
        return rO1.getName().compareTo(rO2.getName());
      } else if (sStyle==SearchSorter.BY_SOME_OTHER_CRITERIA) {
         // ...
      }
    }
    The SearchSorter class could be supplied w. a setter method for a value that
    tells it what sorting style it will use and you can set that value prior to
    each ssrt.

    You use it like this:

    sStyle=SearchSorter.BY_AGE_AND_NAME; // a final static value you have defined in SearchSorter
    searchSorterInstance.setSortStyle(sStyle);
    Collections.sort(arrayListOfRecordObject, searchSorterInstance);

    A new arrayListOfRecordObject is created for each sort, and will serve as
    cache if you use another ArrayList for storing these arraylists.

    mKay ?
    Last edited by sjalle; 07-28-2005 at 11:46 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2005
    Posts
    1
    I'll just add this to what sjalle posted: with a little bit of knowledge about reflection you can easily write a "DynamicComparator" class that can do any comparisons you would generally need based on sort parameters supplied at runtime. I wrote class like this for my employer's web apps so that we can sort query results based on user input without having to write comparators.

    So when I want to sort a Collection based on user input I do something like this:

    DynamicComparator dc = new DynamicComparator("-registrationDate,+firstName,+lastName");
    Collections.sort(myCollection, dc);

    To do something like this you'll have to use some conventions in the objects you want to sort. For instance, my class only works if the specified sort fields have either a get method or are public variables, and unless the fields are Comparables they are converted to Strings and then compared.

    Actually, there is probably a similar comparator class in Jakarta Commons somewhere though I haven't checked.

  4. #4
    Join Date
    Jul 2005
    Posts
    2
    Thanks sjalle, thanks pdo400

    You helped a lot.

    Regards
    Agady

Similar Threads

  1. Recording frequency of search results.
    By Dylan in forum ASP.NET
    Replies: 0
    Last Post: 12-17-2002, 08:35 AM
  2. Search Box Results
    By Brad in forum XML
    Replies: 3
    Last Post: 10-05-2001, 05:11 PM
  3. SQL, Intranet, Saving search results
    By Mikael Gustafsson in forum Database
    Replies: 0
    Last Post: 05-29-2001, 02:39 AM
  4. SQL, Intranet, Saving search results
    By Mikael Gustafsson in forum Database
    Replies: 0
    Last Post: 05-29-2001, 02:39 AM
  5. select and different search results
    By kari in forum VB Classic
    Replies: 1
    Last Post: 05-10-2001, 01:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center