-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
3 - M (<= 1 month)
-
6991
Problem
Hi everyone, I would like to request natural sorting through Realm because my client needs the orders to be sorted ascending naturally by sequence. A sequence is a string that consists of alphanumeric characters, for example 'ABC123'. The clients expect it to be sorted like this: ['ABC123', 'ABC4'] => ['ABC4', 'ABC123']
Currently I'm sorting the Realm.Results from useQuery with useMemo like this:
const orders = useQuery(Order) const sortedOrders = useMemo(() => { const _orders = orders.toJSON() _orders.sort((a, b)=> { return a.sequence.localeCompare(b.sequence, undefined, { numeric: true, sensitivity: 'variant', }) }) return _orders }, [orders])
But this would add a layer of complexity to the query, and although it would solve the problem for now, the client will request more sorting options in the future so it would become increasingly more difficult to sort with useMemo as opposing to passing an array to the sorted function from Realm.
It would be so much better if I can sort it naturally in Realm instead, like this:
const orders = useQuery(Order, collection => { return collection.sorted([ ['sequence', false, true], ]) })
Where the third parameter would be natural?: boolean
Or we can change the sorted function to receive an options object like this:
const orders = useQuery(Order, collection => { return collection.sorted([ { field: 'sequence', descending: false, natural: true, }, ]) })
Hope this gets implemented soon, thank you!
Solution
No response
Alternatives
No response
How important is this improvement for you?
I would like to have it but have a workaround
Feature would mainly be used with
Local Database only