Dave's Brain

Browse - programming tips - java sort arraylist

Date: 2010aug7
Language: java

Q.  How do I sort an ArrayList?

A.

	ArrayList<MyEntry> 	a;

	// Add things to array a

	Collections.sort(a, new customCompare);

Where customCompare is:

	public class customCompare implements java.util.Comparator<Entry> {
		public int compare(MyEntry a, MyEnry b) {
			return a.field1.compareTo(b.field1);
		}
	} 
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: