java.lang.Object
io.jenetics.util.ProxySorter
This sorting methods doesn't sort a given array directly, instead
an index lookup array is returned which allows to access the array in a
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:
- 6.3
- See Also:
- 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
Modifier and TypeClassDescriptionstatic interface
The comparator used for comparing two array elements at the specified indexes. -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]
sort
(double[] array) Sorting the given array by creating an index lookup array.static int[]
sort
(double[] array, int from, int to) 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
(int[] array, int from, int to) 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 int[]
sort
(long[] array, int from, int to) Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]Sorting the given array by creating an index lookup array.static <T> int[]
sort
(BaseSeq<? extends T> array, int from, int to, Comparator<? super T> comparator) 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[]Sorting the given array by creating an index lookup array.static <T extends Comparable<? super T>>
int[]Sorting the given array by creating an index lookup array.static <T> int[]
sort
(List<? extends T> array, int from, int to, Comparator<? super T> comparator) 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 extends Comparable<? super T>>
int[]sort
(T[] array, int from, int to) Sorting the given array by creating an index lookup array.static <T> int[]
sort
(T[] array, int from, int to, Comparator<? super T> comparator) 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 from, int to, ProxySorter.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 Details
-
sort
public static <T> int[] sort(T array, int from, int to, 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, 5, 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 sortedfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sortedcomparator
- the array element comparator- Returns:
- the sorted index array
- Throws:
NullPointerException
- if one of the arrays or comparator isnull
IllegalArgumentException
- iffrom > to
ArrayIndexOutOfBoundsException
- iffrom < 0
- Since:
- 6.3
-
sort
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 arrays isnull
IllegalArgumentException
- iflength < 0
-
sort
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
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
Sorting the given array by creating an index lookup array.- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sortedcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
public static <T> int[] sort(BaseSeq<? extends T> array, int from, int to, 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 sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sortedcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
public static <T extends Comparable<? super T>> int[] sort(BaseSeq<? extends T> array, int from, int to) Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
public static <T> int[] sort(List<? extends T> array, int from, int to, 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 sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sortedcomparator
- the array element comparator- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-
sort
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
public static <T extends Comparable<? super T>> int[] sort(List<? extends T> array, int from, int to) Sorting the given array by creating an index lookup array.- Type Parameters:
T
- the array element type- Parameters:
array
- the array to sortfrom
- the index of the first element (inclusive) to be sortedto
- the index of the last element (exclusive) to be sorted- Returns:
- the sorted index lookup array
- Throws:
NullPointerException
- if the array isnull
IllegalArgumentException
- iffrom > to
IndexOutOfBoundsException
- if the sub-range is out of bounds- Since:
- 6.3
- See Also:
-