Open bao-multiIT opened 10 months ago
It is correct that Realm doesn't support natural sorting order. For the most efficient implementation, we will have to add it to https://github.com/realm/realm-core.
As a sidenote: JavaScript's sort doesn't support natural sorting either.
I'd like to propose this option:
Example sorting with lodash (orderBy)
// Get string for natural sort
export const getOrderString = (string?: string, targetLength = 10): string | null => {
if (typeof string !== 'string') {
return null
}
const parts = _toLower(string).match(/(\d+|[a-zа-я.]+)/g)
const resolvedParts = _map(parts, (part) => {
if (_isNaN(parseInt(part, 10))) {
return part
} else {
return _padStart(part, targetLength, '0')
}
})
return resolvedParts.join('')
}
import { orderBy as _orderBy } from 'lodash'
...
const orders = useQuery(Order)
const sortedOrders = useMemo(() => {
return _orderBy(
orders,
[(order) => getOrderString(order.name)],
['asc'],
)
}, [orders])
Realm
results.sorted(descriptor: (string | [string, boolean])[])
results.sorted(descriptor: string, reverse?: boolean)
Only string type
Lodash
orderBy(
collection (Array|Object): The collection to iterate over.
[iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]): The iteratees to sort by.
[orders] (string[]): The sort orders of iteratees.
)
String, object, function type!!!
Final version:
const orders = useQuery(Order).sorted(
[
(order) => getOrderString(order.name),
'otherField'
],
[
'asc',
'desc'
]
)
This way, the user will have more options for sorting the collection! Among other things, the user will be able to apply natural sorting independently. It's a more versatile solution.
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
withuseMemo
like this: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 thesorted
function from Realm. It would be so much better if I can sort it naturally in Realm instead, like this:Where the third parameter would be
natural?: boolean
Or we can change the
sorted
function to receive anoptions
object like this: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