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| Type | Description | |
|---|---|---|
tableId | string | |
cellId? | string | The |
descending? | boolean | Whether the sorting should be in descending order. |
offset? | number | The number of |
limit? | number | The maximum number of |
| returns | Ids | An array of the sorted |
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
When called with one object argument, the getSortedRowIds method destructures it to make it easier to skip optional parameters.
getSortedRowIds(args: SortedRowIdsArgs): Ids| Type | Description | |
|---|---|---|
args | SortedRowIdsArgs | A |
| returns | Ids | An array of the sorted |
Example
This example retrieves the first sorted Row Id in a Table.
import {createStore} from 'tinybase';
const store = createStore().setTables({
pets: {
fido: {species: 'dog'},
felix: {species: 'cat'},
cujo: {species: 'wolf'},
},
});
console.log(store.getSortedRowIds({tableId: 'pets', limit: 1}));
// -> ['cujo']
Since
v6.1.0