Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > comparator

Reply
Thread Tools

comparator

 
 
Daniel
Guest
Posts: n/a
 
      04-05-2005
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 );
}
}



 
Reply With Quote
 
 
 
 
John McGrath
Guest
Posts: n/a
 
      04-06-2005
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
 
Reply With Quote
 
 
 
 
Daniel
Guest
Posts: n/a
 
      04-07-2005
Thank you, poll() did it!

"John McGrath" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimized comparator ALuPin@web.de VHDL 6 08-25-2005 08:45 AM
counter plus comparator john VHDL 4 11-10-2004 05:41 AM
comparator problem sk VHDL 0 11-03-2004 07:12 PM
Comparator and minimum value address sunil VHDL 4 02-22-2004 08:10 PM
flags vs. comparator Valentin Tihomirov VHDL 5 11-11-2003 05:16 PM



Advertisments