Link to home
Start Free TrialLog in
Avatar of jaggernat
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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Avatar of jaggernat
jaggernat

ASKER

any code would greatl help

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.getName());
}
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
thats just the comparator, you would use that to sort your list

Collections.sort(myItems, new ByName());
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
to reverse order use:

Collections.sort(myItems, Collections.reverseComparator(new ByName()));
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
hi objects

why does it say method reverseComparator is undefined for the type Collections

thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
do you think it should be

 
            Collections.sort(myItems, new OrderEntryComparator());
            Collections.reverseOrder();

thanks
           Collections.sort(myItems, Collections.reverseOrder(new OrderEntryComparator()));
ok
says the method reverseOrder() is not applicable for arguments OrderEntryComparator
sounds like it does not implement Comparator
it does . reverseOrder() method doesnt take any arguments.
see the link i posted above :)
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().compareTo(oe2.getOrderDate()));
            }

}


thanks

the date is of the type java.sql.Date