comparator

Discussion in 'Java' started by Daniel, Apr 5, 2005.

  1. Daniel

    Daniel Guest

    I am testing a Comparator with the PriorityQueue (JDK1.5), and I want it to
    sort ascending. I tested several random numbers, and they work, but when I
    added the number 100 or 1000 to the list, it did not sort right. Any help
    appreciated.

    Also, as a side note, I tried using PriorityQueue without a comparator and
    the javadocs said it would sort to its natural order, but it did not (called
    the iterator() and the list was unordered). Isn't it supposed to sort for
    me?

    Thanks!



    package daniel.test;

    import daniel.comparator.NumberAscComparator;

    import java.util.PriorityQueue;

    public class SmallestNumbers
    {
    public static void main( String[] args )
    {
    PriorityQueue q = new PriorityQueue( 10, new
    NumberAscComparator() );
    q.add( new Integer( 99 ) );
    q.add( new Integer( 88 ) );
    q.add( new Integer( 66 ) );
    q.add( new Integer( 100 ) ); // this makes the list not sort
    right
    q.add( new Integer( 77 ) );
    q.add( new Integer( 200 ) );

    System.out.println( "original: " + q );


    }
    }

    package daniel.comparator;

    import java.util.Comparator;

    public class NumberAscComparator implements Comparator
    {
    public int compare( Object o, Object o2 )
    {
    if ( !(o instanceof Integer ) || !(o2 instanceof Integer ) )
    {
    return 0;
    }
    Integer i = (Integer)o;
    Integer i2 = (Integer)o2;

    return i.compareTo( i2 );
    }
    }
    Daniel, Apr 5, 2005
    #1
    1. Advertising

  2. Daniel

    John McGrath Guest

    On 4/5/2005 at 4:12:48 PM, Daniel wrote:

    > I am testing a Comparator with the PriorityQueue (JDK1.5), and I want it
    > to sort ascending. I tested several random numbers, and they work, but
    > when I added the number 100 or 1000 to the list, it did not sort right.
    > Any help appreciated.
    >
    > Also, as a side note, I tried using PriorityQueue without a comparator
    > and the javadocs said it would sort to its natural order, but it did not
    > (called the iterator() and the list was unordered). Isn't it supposed to
    > sort for me?


    Try pulling the objects from the PriorityQueue using poll(). They are not
    maintained in order, but they do come out in order.

    --
    Regards,

    John McGrath
    John McGrath, Apr 6, 2005
    #2
    1. Advertising

  3. Daniel

    Daniel Guest

    Thank you, poll() did it!

    "John McGrath" <> wrote in message
    news:...
    > On 4/5/2005 at 4:12:48 PM, Daniel wrote:
    >
    >> I am testing a Comparator with the PriorityQueue (JDK1.5), and I want it
    >> to sort ascending. I tested several random numbers, and they work, but
    >> when I added the number 100 or 1000 to the list, it did not sort right.
    >> Any help appreciated.
    >>
    >> Also, as a side note, I tried using PriorityQueue without a comparator
    >> and the javadocs said it would sort to its natural order, but it did not
    >> (called the iterator() and the list was unordered). Isn't it supposed to
    >> sort for me?

    >
    > Try pulling the objects from the PriorityQueue using poll(). They are not
    > maintained in order, but they do come out in order.
    >
    > --
    > Regards,
    >
    > John McGrath
    Daniel, Apr 7, 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. Valentin Tihomirov

    flags vs. comparator

    Valentin Tihomirov, Nov 10, 2003, in forum: VHDL
    Replies:
    5
    Views:
    612
    Valentin Tihomirov
    Nov 11, 2003
  2. sunil
    Replies:
    4
    Views:
    751
    Ralf Hildebrandt
    Feb 22, 2004
  3. sk

    comparator problem

    sk, Nov 3, 2004, in forum: VHDL
    Replies:
    0
    Views:
    831
  4. john

    counter plus comparator

    john, Nov 8, 2004, in forum: VHDL
    Replies:
    4
    Views:
    747
    Raghavendra
    Nov 10, 2004
  5. Replies:
    6
    Views:
    491
Loading...

Share This Page