Package io.jenetics.util
Class ProxySorter
- java.lang.Object
-
- io.jenetics.util.ProxySorter
-
public final class ProxySorter extends Object
This sorting methods doesn't sort a given array directly, instead an index lookup array is returned which allows to access the array in an sorted order.The minimal requirement of the proxy-sorter will be an access function and the number of elements you want to sort.final double[] array = new Random().doubles(100).toArray(); final int[] proxy = ProxySorter.sort(array); // 'Classical' array sort. final double[] sorted = array.clone(); Arrays.sort(sorted); // Iterating the array in ascending order. for (int i = 0; i < array.length; ++i) { assert sorted[i] == array[proxy[i]]; }
final IntFunction<String> access = ...; final int length = 100; final int[] proxy = ProxySorter.sort( access, length, (a, i, j) -> a.apply(i).compareTo(a.apply(j)) );
- Since:
- 5.1
- Version:
- 5.1
- See Also:
sort(Object, int, Comparator)
,ProxySorter.Comparator
- API Note:
- The most general sorting method is
sort(Object, int, Comparator)
. All other sorting methods can be created with this method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ProxySorter.Comparator<T>
The comparator used for comparing two array elements at the specified indexes.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]
sort(double[] array)
Sorting the given array by creating an index lookup array.static int[]
sort(int[] array)
Sorting the given array by creating an index lookup array.static int[]
sort(long[] array)
Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]sort(BaseSeq<? extends T> array)
Sorting the given array by creating an index lookup array.static <T> int[]
sort(BaseSeq<? extends T> array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]sort(List<? extends T> array)
Sorting the given array by creating an index lookup array.static <T> int[]
sort(List<? extends T> array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]sort(T[] array)
Sorting the given array by creating an index lookup array.static <T> int[]
sort(T[] array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.static <T> int[]
sort(T array, int length, ProxySorter.Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.
-
-
-
Method Detail
-
sort
public static <T> int[] sort(T array, int length, ProxySorter.Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array. The original array is not touched and the returned array can then be used for iterating the array in ascending order.final double[] array = ...; final int[] sorted = ProxySorter.sort( array, array.length, (a, i, j) -> Doubler.compare(a[i], a[j]) ); for (int i : sorted) { System.out.println(array[i]); }
- Type Parameters:
T
- the array type- Parameters:
array
- the array which is sortedlength
- the array lengthcomparator
- the array element comparator- Returns:
- the sorted index array
- Throws:
NullPointerException
- if one of the array isnull
-
sort
public static int[] sort(int[] array)
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static int[] sort(long[] array)
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static int[] sort(double[] array)
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T> int[] sort(T[] array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T extends Comparable<? super T>> int[] sort(T[] array)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T> int[] sort(BaseSeq<? extends T> array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T extends Comparable<? super T>> int[] sort(BaseSeq<? extends T> array)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T> int[] sort(List<? extends T> array, Comparator<? super T> comparator)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
- See Also:
sort(Object, int, Comparator)
-
sort
public static <T extends Comparable<? super T>> int[] sort(List<? extends T> array)
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sort- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
- See Also:
sort(Object, int, Comparator)
-
-