on 01-23-2008 8:10 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.