Logo

Programming-Idioms

History of Idiom 28 > diff from v34 to v35

Edit summary for version 35 by programming-idioms.org:
[Java] Property name is p. +DocURL

Version 34

2016-12-11, 20:49:52

Version 35

2016-12-11, 21:15:01

Idiom #28 Sort by a property

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

Idiom #28 Sort by a property

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

Imports
import java.util.Arrays;
import java.util.Comparator;
Imports
import java.util.Arrays;
import java.util.Comparator;
Code
Arrays.sort(items, new Comparator<Item>(){
	public int compare(Item a, Item b){
		return a.birth - b.birth;
	}
});
Code
Arrays.sort(items, new Comparator<Item>(){
	public int compare(Item a, Item b){
		return a.p - b.p;
	}
});
Comments bubble
items is an array of type Item[].
Use an anonymous class which implements Comparator<Item>
Comments bubble
items is an array of type Item[].
Use an anonymous class which implements Comparator<Item>
Doc URL
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)
Demo URL
http://ideone.com/gqNVEH
Demo URL
http://ideone.com/gqNVEH