So I chose for a List.
List myList = new ArrayList();
myList.add( new Bid(1, 10.0, 5) );
myList.add( new Bid(2, 100.0, 51) );
myList.add( new Bid(3, 1.0, 10) );
Now you have an unsorted list.
To sort by price:
java.util.Collections.sort
public int compare(Object o1, Object o2) {
Bid b1 = (Bid)o1;
Bid b2 = (Bid)o2;
return new Double(b1.getPrice()).comp
}
}) ;
To sort by quantity:
java.util.Collections.sort
public int compare(Object o1, Object o2) {
Bid b1 = (Bid)o1;
Bid b2 = (Bid)o2;
return b1.getQuantity()-b2.getQua
}
}) ;
Main Topics
Browse All Topics





by: zzynxPosted on 2005-09-06 at 04:50:15ID: 14828302
>> How can I best store them (i.e. in an Array?),
(yourList, new java.util.Comparator() { areTo(new Double(b2.getPrice()));
(yourList, new java.util.Comparator() { ntity();
Put them in an array or an (Array)List
>> and then how can I sort them?
By this:
java.util.Collections.sort
public int compare(Object o1, Object o2) {
Bid b1 = (Bid)o1;
Bid b2 = (Bid)o2;
return new Double(b1.getPrice()).comp
}
}) ;
or by quantity:
java.util.Collections.sort
public int compare(Object o1, Object o2) {
Bid b1 = (Bid)o1;
Bid b2 = (Bid)o2;
return b1.getQuantity()-b2.getQua
}
}) ;
Of course I assume your Bid class has the getPrice() and getAQuatity() methods