sorting vectors with Date objects

Discussion in 'Java' started by Richard Anderson, Dec 16, 2003.

  1. Hello,

    I am looking for input as to the best way to sort a vector which
    contains objects of type "X". The type has fields of type String,
    java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.

    The main goal is for me to write a method that takes a vector of these
    object types and sort them by the date field. Does anyone have any
    recommendation as to how to solve this issue?

    -Richard
     
    Richard Anderson, Dec 16, 2003
    #1
    1. Advertising

  2. Richard Anderson

    Sudsy Guest

    Richard Anderson wrote:
    > Hello,
    >
    > I am looking for input as to the best way to sort a vector which
    > contains objects of type "X". The type has fields of type String,
    > java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.
    >
    > The main goal is for me to write a method that takes a vector of these
    > object types and sort them by the date field. Does anyone have any
    > recommendation as to how to solve this issue?
    >
    > -Richard


    Collections.sort( Vector, Comparator );

    Vector implements the List interface and for the Comparator you just
    need a class which overrides the compare( Object o1, Object o2 )
    method. Fair enough?
     
    Sudsy, Dec 16, 2003
    #2
    1. Advertising

  3. Richard Anderson

    Roedy Green Guest

    On 15 Dec 2003 16:12:18 -0800, (Richard
    Anderson) wrote or quoted :

    >I am looking for input as to the best way to sort a vector which
    >contains objects of type "X". The type has fields of type String,
    >java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.
    >
    >The main goal is for me to write a method that takes a vector of these
    >object types and sort them by the date field. Does anyone have any
    >recommendation as to how to solve this issue?


    see http://mindprod.com/jgloss/sort.html

    I points you to source for various sorts you can use. When did sort
    show up in the JDK?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Dec 16, 2003
    #3
  4. Richard Anderson

    newszilla Guest

    "Roedy Green" <> wrote in
    message news:...
    > On 15 Dec 2003 16:12:18 -0800, (Richard
    > Anderson) wrote or quoted :


    > I points you to source for various sorts you can use. When did sort
    > show up in the JDK?
    >

    Since 1.2, when the Collections class was added

    Henk van Voorthuijsen
     
    newszilla, Dec 16, 2003
    #4
  5. Okay, I wrote a class that implements the Comparator interface and
    includes a compare method. Here is the code for the class:

    public class DateVectorComparator implements Comparator
    {
    public final int compare ( Object a, Object b )
    {
    return ((Date)a).compareTo((Date)b);
    }
    }

    Then within the code for the main class.....

    List newList = sites.subList(0, sites.size()); // create a new list
    from vector
    Collections.sort(newList, new DateVectorComparator());

    I get a ClassCastException when I run the class at the line
    "Collections.sort". This makes sense since the list is a list of
    objects of type Object[][] and each element in the array is of a
    different type: String, Date, etc.

    Questions, comments, complaints?


    Roedy Green <> wrote in message news:<>...
    > On 15 Dec 2003 16:12:18 -0800, (Richard
    > Anderson) wrote or quoted :
    >
    > >I am looking for input as to the best way to sort a vector which
    > >contains objects of type "X". The type has fields of type String,
    > >java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.
    > >
    > >The main goal is for me to write a method that takes a vector of these
    > >object types and sort them by the date field. Does anyone have any
    > >recommendation as to how to solve this issue?

    >
    > see http://mindprod.com/jgloss/sort.html
    >
    > I points you to source for various sorts you can use. When did sort
    > show up in the JDK?
     
    Richard Anderson, Dec 16, 2003
    #5
  6. Richard Anderson

    P.Hill Guest

    Richard Anderson wrote:

    > return ((Date)a).compareTo((Date)b);


    > I get a ClassCastException when I run the class at the line
    > "Collections.sort". This makes sense since the list is a list of
    > objects of type Object[][] and each element in the array is of a
    > different type: String, Date, etc.


    You need to define a rule for how String sorts in relation Date etc.
    That is what the Comparator implementation is for.
    Does "Paul" come before "the 16 of December" or after?
    yes, of course it can't sort your array, you haven't told it how it.
    Think! Computers do what you tell them. You told it
    to treat everything as if it were a Date. How do expect it
    to do that? It can't.

    You might come up with a compare routine that includes
    the use of instanceof. Look it up.

    Personally I would put something in the list that can
    hold each of the types of objects you want to sort, YMMV.

    HTH,
    -Paul
     
    P.Hill, Dec 16, 2003
    #6
  7. Okay, so I solved the ClassCastException issue by pulling the date
    element out of the array in the compare method within the
    DateVectorComparator class. But....the final list is not sorted.
    Using the debugger in Eclipse IDE and viewing the variables it looks
    like the two objects that are passed into the compare method are
    exactly the same (equal). Can anyone provide some insight as to what
    the heck is going on or what I could be doing wrong?

    -Richard

    Roedy Green <> wrote in message news:<>...
    > On 15 Dec 2003 16:12:18 -0800, (Richard
    > Anderson) wrote or quoted :
    >
    > >I am looking for input as to the best way to sort a vector which
    > >contains objects of type "X". The type has fields of type String,
    > >java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.
    > >
    > >The main goal is for me to write a method that takes a vector of these
    > >object types and sort them by the date field. Does anyone have any
    > >recommendation as to how to solve this issue?

    >
    > see http://mindprod.com/jgloss/sort.html
    >
    > I points you to source for various sorts you can use. When did sort
    > show up in the JDK?
     
    Richard Anderson, Dec 16, 2003
    #7
  8. Richard Anderson wrote:

    > Okay, I wrote a class that implements the Comparator interface and
    > includes a compare method. Here is the code for the class:
    >
    > public class DateVectorComparator implements Comparator
    > {
    > public final int compare ( Object a, Object b )
    > {
    > return ((Date)a).compareTo((Date)b);
    > }
    > }


    That's the right general idea.

    > Then within the code for the main class.....
    >
    > List newList = sites.subList(0, sites.size()); // create a new list
    > from vector
    > Collections.sort(newList, new DateVectorComparator());


    There is no point whatsoever to creating newList for that purpose -- a
    Vector is already a List, so you can pass the original Vector.

    > I get a ClassCastException when I run the class at the line
    > "Collections.sort". This makes sense since the list is a list of
    > objects of type Object[][] and each element in the array is of a
    > different type: String, Date, etc.


    Right. The Comparator used needs to know and account for the types of
    the objects to be compared. If each element of the Vector is of type
    Object[][], and the Dates by which you want to sort are at position
    [1][2], then the Comparator's compare method would look like this:

    public int compare(Object a, Object b) {
    Object[][] arrayA = (Object[][]) a;
    Object[][] arrayB = (Object[][]) b;

    return ((Date) arrayA[1][2]).compareTo(arrayB[1][2]);
    }

    Notes:
    () There is no advantage to be gained by making the Comparator's compare
    method final.
    () There is little advantage to be gained by casting the argument of
    Date.compareTo().


    You commented in a different message that you had a solution that runs
    without error, but doesn't seem to result in a sorted list. If you post
    a complete example then we can probably identify the problem, but
    otherwise we have no reliable way to diagnose it. Prose descriptions of
    your code are not sufficiently detailed for use in debugging.


    John Bollinger
     
    John C. Bollinger, Dec 16, 2003
    #8
    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. bigbinc
    Replies:
    3
    Views:
    403
    Michael Borgwardt
    Nov 18, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,277
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    3
    Views:
    702
    Shadowman
    Mar 26, 2008
  4. J. Muenchbourg

    asp.net date objects vs. classic asp date objects

    J. Muenchbourg, Oct 3, 2003, in forum: ASP General
    Replies:
    1
    Views:
    174
    msnews.microsoft.com
    Oct 3, 2003
  5. Guest
    Replies:
    0
    Views:
    445
    Guest
    Sep 14, 2005
Loading...

Share This Page