Back

Need to sort using natural sort

  • 0
  • Databases
mounir younes
25 Nov, 2023, 10:03

Hello everyone, I am new to appwrite and would like to know if it is possible to sort using Natural sort. In other words, sorting alphabetically would yield "Test1", "Test10", "Test2". What I want is "Test1", "Test2", "Test10". I normally use MongoDB for the database and solve this using the collation setting (on the db level). How is it possible to do that in appwrite?

Thank you for your precious time. I appreciate it!

TL;DR
User wants to know if it is possible to sort using natural sort in Appwrite. They normally use MongoDB and solve this using collation setting. They found references suggesting it's not possible and cannot change the data. Solution: Change the data to sort alphabetically or consider using the code provided for MongoDB.
Drake
25 Nov, 2023, 16:13

You would need to change the data so that it sorts alphabetically.

Out of curiosity, how exactly would you handle this in mongo?

mounir younes
25 Nov, 2023, 19:35

in mongo its very easy. You just specify the collation and make sure to set numericOrdering to true

TypeScript
db.c.find(
   { }, { _id: 0 }
).sort(
  { n: 1 }
).collation( {
  locale: 'en_US',
  numericOrdering: true
} )

The example is from the docs: https://www.mongodb.com/docs/manual/reference/collation/

mounir younes
25 Nov, 2023, 19:43

@Drake What if I cannot change the data? I have user input that I cannot control. And it has to be sorted out naturally. Not being able to do that in appwrite, will not allow me to use the platform. Is there anyway we can solve that?

Drake
25 Nov, 2023, 20:05

Interesting...and this numeric ordering works with other non-numeric text in the string too? Like "text1" and "text10"?

Drake
25 Nov, 2023, 20:11

I found some references that it's not possible 🧐

https://stackoverflow.com/questions/68965441/sort-on-number-that-is-embedded-in-a-string-field-in-mongodb-collection.

If it was just the number in a string, it would be possible, but the extra text is the problem

Drake
25 Nov, 2023, 20:11

One approach could be to have an extra attribute that is populated client side or via an Appwrite function that can be used for sorting

mounir younes
25 Nov, 2023, 20:35

@Drake What I mentioned was not a "suggestion" it is the actual technique I use in my projects, and yes it does sort alphanumeric content like "text 1", "text 10".

mounir younes
25 Nov, 2023, 20:37
mounir younes
25 Nov, 2023, 20:42
Drake
25 Nov, 2023, 20:45

Feel free to submit a feature request with these as reference

mounir younes
25 Nov, 2023, 20:55

To be honest I don't know how to do that. Can you help?

Drake
25 Nov, 2023, 20:57

https://github.com/appwrite/appwrite/issues and click New to create a new issue

mounir younes
25 Nov, 2023, 21:04

thank you

Drake
27 Nov, 2023, 16:50
Drake
27 Nov, 2023, 16:51

As part of the pitch, would you please include why this is important for you to have?

Drake
27 Nov, 2023, 16:51

Understanding your use-case/why is important for us

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more