TinyBase logoTinyBase

getSortedRowIds

The getSortedRowIds method returns the Ids of every Row in a given Table, sorted according to the values in a specified Cell.

getSortedRowIds(
  tableId: string,
  cellId?: string,
  descending?: boolean,
  offset?: number,
  limit?: number,
): Ids
TypeDescription
tableIdstring

The Id of the Table in the Store.

cellId?string

The Id of the Cell whose values are used for the sorting, or undefined to sort by the Row Id itself.

descending?boolean

Whether the sorting should be in descending order.

offset?number

The number of Row Ids to skip for pagination purposes, if any.

limit?number

The maximum number of Row Ids to return, or undefined for all.

returnsIds

An array of the sorted Ids of every Row in the Table.

The sorting of the rows is alphanumeric, and you can indicate whether it should be in descending order. The offset and limit parameters are used to paginate results, but default to 0 and undefined to return all available Row Ids if not specified.

Note that every call to this method will perform the sorting afresh - there is no caching of the results - and so you are advised to memoize the results yourself, especially when the Table is large. For a performant approach to tracking the sorted Row Ids when they change, use the addSortedRowIdsListener method.

If the Table does not exist, an empty array is returned.

Examples

This example retrieves sorted Row Ids in a Table.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {
    fido: {species: 'dog'},
    felix: {species: 'cat'},
  },
});
console.log(store.getSortedRowIds('pets', 'species'));
// -> ['felix', 'fido']

This example retrieves sorted Row Ids in a Table in reverse order.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {
    fido: {species: 'dog'},
    felix: {species: 'cat'},
    cujo: {species: 'wolf'},
  },
});
console.log(store.getSortedRowIds('pets', 'species', true));
// -> ['cujo', 'fido', 'felix']

This example retrieves two pages of Row Ids in a Table.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {
    fido: {price: 6},
    felix: {price: 5},
    mickey: {price: 2},
    tom: {price: 4},
    carnaby: {price: 3},
    lowly: {price: 1},
  },
});
console.log(store.getSortedRowIds('pets', 'price', false, 0, 2));
// -> ['lowly', 'mickey']
console.log(store.getSortedRowIds('pets', 'price', false, 2, 2));
// -> ['carnaby', 'tom']

This example retrieves Row Ids sorted by their own value, since the cellId parameter is undefined.

import {createStore} from 'tinybase';

const store = createStore().setTables({
  pets: {
    fido: {species: 'dog'},
    felix: {species: 'cat'},
    cujo: {species: 'wolf'},
  },
});
console.log(store.getSortedRowIds('pets'));
// -> ['cujo', 'felix', 'fido']

This example retrieves the sorted Row Ids of a Table that does not exist, returning an empty array.

import {createStore} from 'tinybase';

const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
console.log(store.getSortedRowIds('employees'));
// -> []

Since

v2.0.0