jaggernat
asked on
sorting fields in arraylist
hi guys
I have a array list 'myItems' which contains objects of type Object1.
Now Object1 has fields
private String name = "";
private int age=0;
private java.sql.Date date;
set() //for all the three fields
get() //for all the three fields
now
if (condition1 is true)
{
//i want to display all 'names' in the arraylist in ascending order
}
else if (condition2 is true)
{
//i want to display all 'age' in the arraylist in ascending order
}
else if (condition3 is true)
{
//i want to display all 'dates' in the arraylist in ascending order
}
any idea how i can display the 'names' or 'age' or 'dates' in ascending order
thanks
J
I have a array list 'myItems' which contains objects of type Object1.
Now Object1 has fields
private String name = "";
private int age=0;
private java.sql.Date date;
set() //for all the three fields
get() //for all the three fields
now
if (condition1 is true)
{
//i want to display all 'names' in the arraylist in ascending order
}
else if (condition2 is true)
{
//i want to display all 'age' in the arraylist in ascending order
}
else if (condition3 is true)
{
//i want to display all 'dates' in the arraylist in ascending order
}
any idea how i can display the 'names' or 'age' or 'dates' in ascending order
thanks
J
ASKER
any code would greatl help
thanks
J
thanks
J
public class ByName implements Comparator
{
public int compare(Object _a, Object _b)
{
Object1 a = (Object1) _a;
Object1 b = (Object1) _b;
return a.getName().compareTo(b.ge tName());
}
{
public int compare(Object _a, Object _b)
{
Object1 a = (Object1) _a;
Object1 b = (Object1) _b;
return a.getName().compareTo(b.ge
}
ASKER
ok, the above method will return an int value, but i want to return the list of 'names' in ascending order.
Also, is it possible to return the list of names in descending order?
thanks verymuch
J
Also, is it possible to return the list of names in descending order?
thanks verymuch
J
thats just the comparator, you would use that to sort your list
Collections.sort(myItems, new ByName());
Collections.sort(myItems, new ByName());
ASKER
ok, got it, thanks.
will Collections.sort(myItems, new ByName());
display in ascending order? Actually my requirment is i want it to be displayed in Desceding order as well.
any ideas?
thanks
will Collections.sort(myItems, new ByName());
display in ascending order? Actually my requirment is i want it to be displayed in Desceding order as well.
any ideas?
thanks
to reverse order use:
Collections.sort(myItems, Collections.reverseCompara tor(new ByName()));
Collections.sort(myItems, Collections.reverseCompara
ASKER
thanks very much.
i will try this code first thing tomorrow morning.
any ideas with my next question grealty appreciated.
thanks,
https://www.experts-exchange.com/questions/22627444/display-elements-from-List-in-descending-order.html
i will try this code first thing tomorrow morning.
any ideas with my next question grealty appreciated.
thanks,
https://www.experts-exchange.com/questions/22627444/display-elements-from-List-in-descending-order.html
ASKER
hi objects
why does it say method reverseComparator is undefined for the type Collections
thanks
why does it say method reverseComparator is undefined for the type Collections
thanks
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
do you think it should be
Collections.sort(myItems, new OrderEntryComparator());
Collections.reverseOrder() ;
thanks
Collections.sort(myItems, new OrderEntryComparator());
Collections.reverseOrder()
thanks
Collections.sort(myItems, Collections.reverseOrder(n ew OrderEntryComparator()));
ASKER
ok
says the method reverseOrder() is not applicable for arguments OrderEntryComparator
says the method reverseOrder() is not applicable for arguments OrderEntryComparator
sounds like it does not implement Comparator
ASKER
it does . reverseOrder() method doesnt take any arguments.
see the link i posted above :)
ASKER
anyway
this seems to work
Collections.sort(myItems, new OrderNumberComparator());
Collections.reverseOrder() ;
but the date comparision doesnt work. should the date also be compared like :
public class OrderDateComparator implements Comparator
{
public int compare(Object obj1, Object obj2) {
OrderEntry oe1 = (OrderEntry) obj1;
OrderEntry oe2 = (OrderEntry) obj2;
return - (oe1.getOrderDate().compar eTo(oe2.ge tOrderDate ()));
}
}
thanks
this seems to work
Collections.sort(myItems, new OrderNumberComparator());
Collections.reverseOrder()
but the date comparision doesnt work. should the date also be compared like :
public class OrderDateComparator implements Comparator
{
public int compare(Object obj1, Object obj2) {
OrderEntry oe1 = (OrderEntry) obj1;
OrderEntry oe2 = (OrderEntry) obj2;
return - (oe1.getOrderDate().compar
}
}
thanks
ASKER
the date is of the type java.sql.Date
http://java.sun.com/docs/books/tutorial/collections/algorithms/