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 accessing the array in a sorted order.
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]];
}
The minimal requirement of the proxy-sorter will be an access function and the number of elements you want to sort.
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

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    The comparator used for comparing two array elements at the specified indexes.
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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[]
    sort(BaseSeq<? extends T> array)
    Sorting the given array by creating an index lookup array.
    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.
    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[]
    sort(List<? extends T> array)
    Sorting the given array by creating an index lookup array.
    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.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 sorted
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      comparator - the array element comparator
      Returns:
      the sorted index array
      Throws:
      NullPointerException - if one of the arrays or comparator is null
      IllegalArgumentException - if from > to
      ArrayIndexOutOfBoundsException - if from < 0
      Since:
      6.3
    • 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 sorted
      length - the array length
      comparator - the array element comparator
      Returns:
      the sorted index array
      Throws:
      NullPointerException - if one of the arrays is null
      IllegalArgumentException - if length < 0
    • 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 is null
      See Also:
    • sort

      public static int[] sort(int[] array, int from, int to)
      Sorting the given array by creating an index lookup array.
      Parameters:
      array - the array to sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the sub-range is out of bounds
      Since:
      6.3
      See Also:
    • 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 is null
      See Also:
    • sort

      public static int[] sort(long[] array, int from, int to)
      Sorting the given array by creating an index lookup array.
      Parameters:
      array - the array to sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 is null
      See Also:
    • sort

      public static int[] sort(double[] array, int from, int to)
      Sorting the given array by creating an index lookup array.
      Parameters:
      array - the array to sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 sort
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      See Also:
    • sort

      public static <T> int[] sort(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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 is null
      See Also:
    • sort

      public static <T extends Comparable<? super T>> int[] sort(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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 sort
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 is null
      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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 sort
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      comparator - the array element comparator
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also:
    • 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 is null
      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 sort
      from - the index of the first element (inclusive) to be sorted
      to - the index of the last element (exclusive) to be sorted
      Returns:
      the sorted index lookup array
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if from > to
      IndexOutOfBoundsException - if the subrange is out of bounds
      Since:
      6.3
      See Also: