We help IT Professionals succeed at work.

sorting fields in arraylist

jaggernat
jaggernat asked
on
1,337 Views
Last Modified: 2008-04-24
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
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
any code would greatl help

thanks
J
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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());
}

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
thats just the comparator, you would use that to sort your list

Collections.sort(myItems, new ByName());

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
to reverse order use:

Collections.sort(myItems, Collections.reverseComparator(new ByName()));

Author

Commented:
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/Programming/Languages/Java/Q_22627444.html

Author

Commented:
hi objects

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

thanks
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
do you think it should be

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

thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

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

Author

Commented:
ok
says the method reverseOrder() is not applicable for arguments OrderEntryComparator
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sounds like it does not implement Comparator

Author

Commented:
it does . reverseOrder() method doesnt take any arguments.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
see the link i posted above :)

Author

Commented:
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

Author

Commented:
the date is of the type java.sql.Date

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.