PageRenderTime 23ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/bitbucket/jdk7fun/Arrays2.java

https://bitbucket.org/blob79/jdk7fun
Java | 26 lines | 19 code | 7 blank | 0 comment | 0 complexity | 7f62ff105dffedd680da05ecb38e3793 MD5 | raw file
 1package org.bitbucket.jdk7fun;
 2
 3import java.util.*;
 4import com.google.common.collect.*;
 5
 6class Arrays2 {
 7
 8	public static <T, U extends Comparable<? super U>> void sortBy(T[] array, Extractor<T, U> extractor) {
 9		sortBy(extractor).sort(array);		
10	}		
11
12	public static <T, U extends Comparable<? super U>> SortBuilder<T> sortBy(Extractor<T, U> extractor) {
13		return new SortBuilder<T>().and(extractor);
14	}
15
16	public static class SortBuilder<T>{
17		private Ordering<T> ordering =  #{left, right -> 0};
18		
19		public  <U extends Comparable<? super U>> SortBuilder<T> and(Extractor<T, U> extractor){
20 			ordering = ordering.compound(#{T left, T right -> extractor.get(left).compareTo(extractor.get(right))});
21			return this;
22		}
23
24		public void sort(T[] array){ Arrays.sort(array, ordering); }
25	}
26}