realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.69k stars 564 forks source link

Natural sorting #6141

Open bao-multiIT opened 10 months ago

bao-multiIT commented 10 months ago

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

kneth commented 9 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.

astrahov commented 1 month ago

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.