Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Sorting List based on object's members (http://www.velocityreviews.com/forums/t143977-sorting-list-based-on-objects-members.html)

- 06-05-2005 01:00 PM

Sorting List based on object's members
 
I have a List "innerSomeClasses" that I want to sort based on name or
date. Please give me some pointers to accomplish it.

Use sort(List<T> list, Comparator<? super T> c)? If so, how?


class SomeClass {

private List innerSomeClasses = createInnerSomeClasses();

....

public SomeClass() {
...
}

public void sort(int type, boolean reverse) {
// How?
}

private List createInnerSomeClasses() {
....
}

...

class InnerSomeClass {

public static final int SORT_BY_NAME = 1;
public static final int SORT_BY_DATE = 2;

public InnerSomeClass() {
....
}

public String getName() {
return name;
}

public Date getDate() {
return date;
}

...
}
}

shakah 06-05-2005 02:11 PM

Re: Sorting List based on object's members
 
- wrote:
> I have a List "innerSomeClasses" that I want to sort based on name or
> date. Please give me some pointers to accomplish it.
>
> Use sort(List<T> list, Comparator<? super T> c)? If so, how?
>
>
> class SomeClass {
>
> private List innerSomeClasses = createInnerSomeClasses();
>
> ....
>
> public SomeClass() {
> ...
> }
>
> public void sort(int type, boolean reverse) {
> // How?
> }
>
> private List createInnerSomeClasses() {
> ....
> }
>
> ...
>
> class InnerSomeClass {
>
> public static final int SORT_BY_NAME = 1;
> public static final int SORT_BY_DATE = 2;
>
> public InnerSomeClass() {
> ....
> }
>
> public String getName() {
> return name;
> }
>
> public Date getDate() {
> return date;
> }
>
> ...
> }
> }


Kind of rushing through this one, so my apologies in advance if this
misses the mark, but if you make an inner class of SomeClass that
implements Comparator:
private static class InnerClassComparator
implements java.util.Comparator {
private boolean bByDate_ ;
private boolean bIncreasing_ ;

public InnerClassComparator(boolean bIncreasing, boolean bByDate) {
bIncreasing_ = bIncreasing ;
bByDate_ = bByDate ;
}

public int compare(Object o1, Object o2) {
int nReturn = 0 ;

if(null==o1 && null==o2) {
}
else if(null==o1) {
nReturn = -1 ;
}
else if(null==o2) {
nReturn = 1 ;
}
else {
InnerSomeClass iscOne = (InnerSomeClass) o1 ;
InnerSomeClass iscTwo = (InnerSomeClass) o2 ;
if(bByDate_) {
nReturn = iscOne.getDate().compareTo(iscTwo.getDate()) ;
}
else {
nReturn = iscOne.getName().compareTo(iscTwo.getName()) ;
}
}

return bIncreasing_ ? nReturn : (-1 * nReturn) ;
}

public boolean equals(Object o) {
return bByDate_==((InnerClassComparator) o).bByDate
&& bIncreasing_ == ((InnerClassComparator) o).bIncreasing_
;
}
}

you can use something like the following in your sort method:
java.util.Collections.sort(
innerSomeClasses
,new InnerClassComparator(InnerSomeClass.SORT_BY_DATE, true)
) ;


John McGrath 06-05-2005 06:27 PM

Re: Sorting List based on object's members
 
On 6/5/2005 at 10:11:35 AM, shakah wrote:

> if you make an inner class of SomeClass that
> implements Comparator:
> private static class InnerClassComparator


Minor point:

This is not an /inner/ class, but rather a /nested/ class. As defined
in the Java Language Specification, inner classes cannot be static.

--
Regards,

John McGrath


All times are GMT. The time now is 12:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.