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

...
}
}
 
S

shakah

- said:
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)
) ;
 
J

John McGrath

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top