Class GridSorter

java.lang.Object
com.codename1.ui.table.GridSorter

public final class GridSorter extends Object

The sort a DataGrid orders its rows with, for any other list of rows.

It orders positions rather than data: each sort key is read once per row into an array before sorting, so a comparison never goes back to the model, and the sort is stable, so rows that compare equal keep the order they came in. A million rows cost a million reads per key.

String[] names = {"dana", "Ann", "carl"};
int n = names.length;
Object[][] keys = new Object[1][n];
for (int i = 0; i < n; i++) {
    keys[0][i] = names[i];
}
int[] order = GridSorter.order(n, keys, null, new boolean[]{true});
// order[k] is the position that sorts k-th
  • Method Details

    • order

      public static int[] order(int count, Object[][] keys, Comparator[] comparators, boolean[] ascending)

      Orders the positions 0..count-1 by their keys.

      Parameters
      • count: how many positions there are
      • keys: keys[k][i], the value of sort key k at position i; the first key decides, the next one breaks its ties, and so on
      • comparators: a comparator per key, or null; a null array or a null entry compares values naturally: numbers by value, dates by time, booleans false first, anything else by its text ignoring case
      • ascending: the direction of each key

      A null value sorts before any other, and after them all when its key descends. A comparator is never given a null.

      Returns

      the positions in sorted order

    • compare

      public static int compare(Comparator comparator, Object a, Object b)
      Compares two values the way order does for an ascending key.