Logo

Programming-Idioms

History of Idiom 28 > diff from v2 to v3

Edit summary for version 3 by :

Version 2

2015-04-07, 14:07:21

Version 3

2015-04-07, 14:13:35

Idiom #28 Sort by a property

Sort items x of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Idiom #28 Sort by a property

Sort items x of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Imports
import java.util.Collections;
import java.util.Comparator;
Code
Collections.sort(items, new Comparator<Item>(){
	public int compare(Item a, Item b){
		return a.birth - b.birth;
	}
});
Comments bubble
items is a List<Item>.
Use an anonymous class which implements Comparator<Item>