Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    35

    Unanswered: List Sorting in JSP

    I need help to sort this list to be alphabetically
    I have an array list in this list name have different categories and color. I need to sort it by cotegories, color and sort by ascending order

    the out come will be like this Ami is name, cat is categories and color is blue..(color that I mention is color font)
    Ami-cat-blue
    Aki-cat-blue
    Bobo-cat-blue
    Bobby-dog-yellow
    riki-dog-yellow


    Code:
    for( int i=0; i < records.size(); i++) {
    
                   if (((String) (((Attributes) records.elementAt(i)).get("animaltype").get())).equals("cat")) {
                       catList.add(records.elementAt(i));
    
                   } else if (((String) (((Attributes) records.elementAt(i)).get("animaltype").get())).equals("dogs")) {
                       dogsList.add(records.elementAt(i));
                   }
    
    records.removeAllElements();
    
                records.addAll(catList);
                records.addAll(dogsList);
    now I can sort it by color and categories but how to make name list in ascending order in jsp
    Need HELP...thank you
    **akuccputsedut**
    Intelligent is Me!!But a little confusing
    sometimes needs HELP!!

  2. #2
    Join Date
    Jan 2005
    Posts
    362
    You mean this?

  3. #3
    Join Date
    Aug 2009
    Posts
    68
    Maybe you could declare a method "compareTo" in your "Attribute" class:

    Code:
    int compareTo(Attribute toComp) {
     String s1 = toComp.animalName + "-" + toComp.animalType + "-" + toComp.color;
     String s2 = animalName + "-" + animalType + "-" + color;
     return s1.compareTo(s2);
    }
    Then you can sort Attribute as if they were String(s).

    Hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •