Case-insensitive sort
Hi. I'm trying to use query.order, but it seems like it is case-sensitive (A... comes before a...). Is there anyway to make the query case-insensitive (a... and A... are treated the same, but they come before b...)? Thanks!
There isn't an officially supported way to do this right now. But we have ideas for how to support these sorts of transformations in the query api in the future.
For now you have a few options:
-
Sort on your results in javascript - this is simple but you won't be able to leverage additional apis in the query engine like
limit -
Manually maintain a computed attribute Support for computed attributes is on our short term roadmap, but for now you need to manage them manually. You'll create a new attribute
attr_lowerand you'll have to manually maintain the invariant thatattr_lower === lowercase(attr). Specifically when you mutateattrwithinsertandupdateyou'll need to manually mutate theattr_lowerattribute.
const text = 'AbC';
await client.insert('collection', { attr: text, attr_lower: lowercase(text) });
await client.update('collection', 'id', (entity) => {
const updatedText = 'XyZ';
entity.attr = updatedText;
entity.attr_lower = lowercase(updatedText);
});
That should allow you to fully use the query engine client.query('collection').order('attr_lower', 'ASC').limit(10).
We discussed this approach in our Discord recently as well: https://discord.com/channels/1138467878623006720/1138467879113728033/1256648644736712715
Other features on our short term roadmap like triggers could also be helpful here eventually.
I'll leave this open for others to share any ideas.
related to this: localeCompare for numerical string sorting
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare