cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting data in collection in alphabetical order

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

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

You can write your own comparator. Just create an object that implements the Comparable interface. Then, you will be forced by the compiler to implement the compare method.

In this implementation, you can compare the 2 objects.

You pass in instance of the comparator to the Colections.sort(Comparator c) method.

Context nodes also have a sort method that take a comparator as parameter.

Good luck,

Roelof

Former Member
0 Kudos

It's actually even more simple.

The collections sort has a sort method that takes a List and sorts that list according to its natural order.

Roelof

Former Member
0 Kudos

Hi Roelof,

i would like to know in a more detailed way.The problem here is inside the collection the elements appear as

<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>

and how can i take the title alone and compare with the other elements in the collection and sort accordingly.Kindly provide your valuable suggestion .

Thanks and Regards

Neeta

Former Member
0 Kudos

In that case you must follow the first approach I mentioned. Create the comparator, e.g. AspectRowComparator, in the compare method, you compare 2 objects.

You cast the 2 Object parameters to your AspectRow structure. Then you compare the attributre title of both objects and return the result. It is convenient to use the compare method of the Comparable interface.

e.g.

return ((Comparable) ((AspectRowBean) o1).getTitle()).compareTo(((AspectRowBean) o2).getTitle())

I only wonder how to parse the ill formed XML to a bean. That is, there is no attribute named title in the XML.

Good luck,

Roelof