Sorting List based on object's members

Discussion in 'Java' started by -, Jun 5, 2005.

  1. -

    - Guest

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

    ...
    }
    }
    -, Jun 5, 2005
    #1
    1. Advertising

  2. -

    shakah Guest

    - 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)
    ) ;
    shakah, Jun 5, 2005
    #2
    1. Advertising

  3. -

    John McGrath Guest

    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
    John McGrath, Jun 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Biranchi Narayan Panda
    Replies:
    0
    Views:
    313
    Biranchi Narayan Panda
    Feb 18, 2010
  2. Biranchi Narayan Panda
    Replies:
    0
    Views:
    355
    Biranchi Narayan Panda
    Feb 21, 2010
  3. Biranchi Narayan Panda
    Replies:
    2
    Views:
    1,051
    Bernd Wechner
    May 11, 2010
  4. Biranchi Narayan Panda

    window based authentication with members defined in a distribution list.

    Biranchi Narayan Panda, Feb 21, 2010, in forum: ASP .Net Security
    Replies:
    2
    Views:
    817
    Bernd Wechner
    May 11, 2010
  5. Biranchi Narayan Panda
    Replies:
    0
    Views:
    720
    Biranchi Narayan Panda
    Feb 21, 2010
Loading...

Share This Page