Skip to Content
author's profile photo Former Member
Former Member

Sorting a collection with least time complexity algorithm

Hi ,

I have a collection which is having data

<AspectRow>

key=2732ddb1-adf5-11dc-c76c-0013210f9b73

createdBy=Administrator

createdAt=2007-12-19 11:12:15.051

lastChangedBy=Administrator

lastChangedAt=2007-12-19 11:12:15.071

title=cc

description=cc

type=Rating

isActive=1

isDefault=true

</AspectRow>

, <AspectRow>

key=2c37f611-adf5-11dc-ce26-0013210f9b73

createdBy=Administrator

createdAt=2007-12-19 11:12:23.473

lastChangedBy=Administrator

lastChangedAt=2007-12-19 11:12:23.503

title=bb

description=dd

type=Rating

isActive=1

isDefault=true

</AspectRow>

<AspectRow>

key=2c37f611-adf5-11dc-ce26-0013210f9b73

createdBy=Administrator

createdAt=2007-12-19 11:12:23.473

lastChangedBy=Administrator

lastChangedAt=2007-12-19 11:12:23.503

title=bb

description=a

type=Rating

isActive=1

isDefault=true

</AspectRow>

<AspectRow>

key=2c37f611-adf5-11dc-ce26-0013210f9b73

createdBy=Administrator

createdAt=2007-12-19 11:12:23.473

lastChangedBy=Administrator

lastChangedAt=2007-12-19 11:12:23.503

title=bb

description=b

type=Rating

isActive=1

isDefault=true

</AspectRow>

i would like to know how can i sort the collection based on the alphabetical order of atrribute title.

Thanks and Regards

Neeta

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 23, 2008 at 08:44 AM

    Assuming that you want to do this in Java, you will first need to parse the data into Java objects. After that, you assemble these into a java.util.List and then you can use the built-in sort() method to do the sorting (it uses a variant of the "Mergesort" algorithm):

    import java.util.Comparator;
    import java.util.List;
    import java.util.Collections;
    
    class AspectRow {
      ...
      String getTitle() {
        ...
      }
      ...
    }
    
    class AspectRowComparator implements Comparator<AspectRow> {
    
      public int compare(AspectRow aspectRow1, AspectRow aspectRow2) {
        return aspectRow1.getTitle().compareTo(aspectRow2.getTitle());
      }
      
    }
    
    public class SortAspectRows {
    
      public void sort(List<AspectRow> aspectRows) {
    
        Collections.sort(aspectRows, new AspectRowComparator());
    
      }
      
    }
    

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Well, first of all your code contains a few syntactic problems such as the capitalized keywords "Public" and "Class" and the fact that your code should to be contained in a method.

      Other than that, I get a compilation error at the same place as you, but for a different reason; my compiler says something like "'sort(java.util.List, java.util.Comparator)' cannot be applied to '(java.util.Colletion, TitleComparator')". This can easily be fixed by declaring the 'criteries' variable as a java.util.List instead of a mere java.util.Collection.

      Hope this helps; best regards,

      Jens

      P.S.: I am not sure that calling sort() in a loop after every add() is more efficient than first adding every element in the loop and then calling sort() only once after the execution of the loop.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.