Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inverse sort on key?

Reply
Thread Tools

inverse sort on key?

 
 
nooneinparticular314159@yahoo.com
Guest
Posts: n/a
 
      08-05-2008
I have an object (say, Car). Car contains some values, one of which
is speed. I want to store lots of cars in some sort of object that
will keep them sorted by speed. Some cars may have the same speed.
The catch is that I want to sort by highest speed, not lowest speed.
If I insert lots of cars into a TreeSet, with the key being the speed,
I get them in increasing order. If there are n cars with the same
speed, I get only one, because the keys are identical. I've been
unable to find an object that would work. I've tried implementing a
compare method in my object, and that doesn't help either. Any ideas?

To summarize, the container object must:
1) Support sorting in inverse order (or another order of my choice)
2) Keep the objects sorted.
3) Support duplicate objects.

(I suppose I could sort on the negative value of the sort key, but...)

Thanks!
 
Reply With Quote
 
 
 
 
Daniele Futtorovic
Guest
Posts: n/a
 
      08-05-2008
On 05/08/2008 20:22, http://www.velocityreviews.com/forums/(E-Mail Removed) allegedly wrote:
> I have an object (say, Car). Car contains some values, one of which
> is speed. I want to store lots of cars in some sort of object that
> will keep them sorted by speed. Some cars may have the same speed.
> The catch is that I want to sort by highest speed, not lowest speed.
> If I insert lots of cars into a TreeSet, with the key being the speed,
> I get them in increasing order. If there are n cars with the same
> speed, I get only one, because the keys are identical. I've been
> unable to find an object that would work. I've tried implementing a
> compare method in my object, and that doesn't help either. Any ideas?
>
> To summarize, the container object must:
> 1) Support sorting in inverse order (or another order of my choice)
> 2) Keep the objects sorted.


Specify a proper Comparator. A TreeSet should do just fine. See example
code below.

> 3) Support duplicate objects.


I quite certain that's not a problem of order (Comparable/Comparator),
but rather one of _equality_. Do you override equals() in that class?
Does the overridden method specify that two Cars are equal if their
speeds are equal? If yes, there's your problem. If not, I think it's
somewhere in the vicinity.



<code what_about_the_javadoc="shrug">
public class InverseComparator<E>
implements Comparator<E>
{
private Comparator<E>
backingComparator
;

public InverseComparator(){
}

public InverseComparator(Comparator<E> normalorder){
backingComparator = normalorder;
}

public int compare(E o1, E o2) {
if( backingComparator != null ){
return - backingComparator.compare(o1, o2);
}
else{
if( ! (o1 instanceof Comparable) ){
throw new IllegalArgumentException("No normal order
Comparator specified -- Objects must implement Comparable");
}

return - ((Comparable)o1).compareTo(o2);
}
}



public static void main(String[] ss){
List<Car> cars = Arrays.asList( new Car[]{ new Car(2),new
Car(3),new Car(5),new Car(7),new Car(11) } );

List<Car> cars1 = new ArrayList<Car>(cars);
List<Car> cars2 = new ArrayList<Car>(cars);
List<Car> cars3 = new ArrayList<Car>(cars);
List<Car> cars4 = new ArrayList<Car>(cars);

Collections.sort(cars1);
Collections.sort(cars2, new InverseComparator<Car>());
Collections.sort(cars3, new NormalOrderCarComparator());
Collections.sort(cars4, new InverseComparator<Car>(new
NormalOrderCarComparator()));

System.out.println(cars1);
System.out.println();
System.out.println(cars2);
System.out.println();
System.out.println(cars3);
System.out.println();
System.out.println(cars4);
System.out.println();
}

private static class Car
implements Comparable<Car>
{
int speed;

public Car(int speed){
this.speed = speed;
}

public int compareTo(Car o) {
return speed - o.speed;
}

public String toString(){
return "Car[speed=" + speed + "]";
}
}

private static class NormalOrderCarComparator
implements Comparator<Car>
{
public int compare(Car o1, Car o2) {
return o1.speed - o2.speed;
}
}
}

</code>


--
DF.
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      08-05-2008
(E-Mail Removed) wrote:
> I have an object (say, Car). Car contains some values, one of which
> is speed. I want to store lots of cars in some sort of object that
> will keep them sorted by speed. Some cars may have the same speed.
> The catch is that I want to sort by highest speed, not lowest speed.
> If I insert lots of cars into a TreeSet, with the key being the speed,
> I get them in increasing order. If there are n cars with the same
> speed, I get only one, because the keys are identical. I've been
> unable to find an object that would work. I've tried implementing a
> compare method in my object, and that doesn't help either. Any ideas?
>
> To summarize, the container object must:
> 1) Support sorting in inverse order (or another order of my choice)
> 2) Keep the objects sorted.
> 3) Support duplicate objects.
>
> (I suppose I could sort on the negative value of the sort key, but...)
>
> Thanks!


You can keep the objects in a sorted array using a comparator to sort on
whatever field you want. Duplicate objects implies duplicate keys which
I don't think is possible with Maps or Sets so some sort of array you
keeps sorted is probably your best bet.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-05-2008
On Tue, 5 Aug 2008 11:22:16 -0700 (PDT),
"(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>I have an object (say, Car). Car contains some values, one of which
>is speed. I want to store lots of cars in some sort of object that
>will keep them sorted by speed. Some cars may have the same speed.
>The catch is that I want to sort by highest speed, not lowest speed.
>If I insert lots of cars into a TreeSet, with the key being the speed,
>I get them in increasing order. If there are n cars with the same
>speed, I get only one, because the keys are identical. I've been
>unable to find an object that would work. I've tried implementing a
>compare method in my object, and that doesn't help either. Any ideas?
>
>To summarize, the container object must:
>1) Support sorting in inverse order (or another order of my choice)
>2) Keep the objects sorted.
>3) Support duplicate objects.
>
>(I suppose I could sort on the negative value of the sort key, but...)
>
>Thanks!


see
http://mindprod.com/jgloss/comparator.html#REVERSEORDER
http://mindprod.com/jgloss/treeset.html


--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
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
Query about tan inverse function jahaya@gmail.com VHDL 1 07-17-2005 01:27 PM
Inverse of 'chop @array' gusmeister Perl 1 03-06-2004 01:28 AM
Re: CEP vs Inverse Multiplexing Andre Beck Cisco 0 11-21-2003 05:27 PM
CEF vs MLPPP vs Inverse Multiplex SysAdmin Cisco 1 11-20-2003 01:51 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments