cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting a collection with least time complexity algorithm

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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());

  }
  
}

Former Member
0 Kudos

Hi Jens,

I tried writing my code as below

Public class A

{

Collection criteries =new ArrayList();

for( int indexContext = 0; indexContext < criteriaContextAspect.size(); indexContext++) {

IAspectRow contextRow = criteriaContextAspect.getAspectRow(indexContext);

IAspect criteriaAspect = contextRow.getRelatedAspect(Constants.CRITERIES_RELATION_NAME);

criteries.add(criteriaAspect.getAspectRow(0));

Collections.sort(criteries,new TitleComparator());

}

Class TitleComparator implements Comparator

{

public int compare(Object o1,Object o2)

{

IAspectRow row1 =(IAspectRow)o1;

IAspectRow row2 =(IAspectRow)o2;

String t1= row1.getAttributeValue("title")

..................................................

}

Here when i compile i am getting an error Cannot resolve symbol criteries in

Collections.sort(criteries,new TitleComparator());

Kindly suggest what can be the reason for this....

regards

neeta

Former Member
0 Kudos

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.

Answers (0)