Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

sorting an arraylist in descending order

807600May 31 2007 — edited May 31 2007
Hi,

I'm working with an arraylist, PO (private ArrayList <Book> myPO;). It contains a number of isbn values and my goal is to sort them in descending order.

This is how I'm doing it now:
public void isbnsort()
    {
        ArrayList temp = new ArrayList<Book>();
        temp = myPO;
        int counter = 0;
 
		for(int i=0; i<myPO.size()-1; i++)
		{
			for(int k=0; i<myPO.size()-1; k++)
			{
	
				if(myPO.get(i).value()>=myPO.get(k).value())
                counter++;
			}
      
			if(counter == myPO.size())
			{
				temp.add(myPO.get(i));
				myPO.remove(i);
				i = 0;
			}
		}
 
		myPO = temp;
	}
I don't know if this is the right way to do it, but there has to be an easier/more efficient way than this, I think. I would appreciate any help on how to do this in a better manner.

Sorry if I've made any silly errors here.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2007
Added on May 31 2007
20 comments
738 views