Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case-insensitive sort #139

Open
thanhnguyen2187 opened this issue Jul 3, 2024 · 2 comments
Open

Case-insensitive sort #139

thanhnguyen2187 opened this issue Jul 3, 2024 · 2 comments

Comments

@thanhnguyen2187
Copy link

thanhnguyen2187 commented Jul 3, 2024

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!

@wernst
Copy link
Contributor

wernst commented Jul 3, 2024

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:

  1. 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

  2. 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_lower and you'll have to manually maintain the invariant that attr_lower === lowercase(attr). Specifically when you mutateattr with insert and update you'll need to manually mutate the attr_lower attribute.

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.

@sgup
Copy link
Contributor

sgup commented Aug 6, 2024

related to this: localeCompare for numerical string sorting

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants