triplit icon indicating copy to clipboard operation
triplit copied to clipboard

Case-insensitive sort

Open thanhnguyen2187 opened this issue 1 year ago • 2 comments

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!

thanhnguyen2187 avatar Jul 03 '24 16:07 thanhnguyen2187

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.

wernst avatar Jul 03 '24 18:07 wernst

related to this: localeCompare for numerical string sorting

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

sgup avatar Aug 06 '24 00:08 sgup