Cloud Firestore Case insensitive query sorting

I tried reading the sorted data from Cloud Firestore using OrderBy . And Firestore returned the data in the following order:

AAA
IN
aah
BBB

Now I want something like the following:

AAA
aah
IN
BBB

I want this result to be used only with OrderBy, not for manual sorting.
Is there a way to sort this in Firestore?


Please provide me a solution for this.

Thanks at Advance.

+5
source share
2 answers

Cloud Firestore sorting is case sensitive. There is no flag for the sort to ignore the case.

The only way to get your use case is to save the field twice.

Say your field that stores "AAA" and "aaa" is called myData . In the client code, you need to save the second field named myData_insensitive , where you store a copy of the data that you converted to lowercase.

 DocA: -> myData = 'AAA' -> myData_insensitive = 'AAA' DocB: -> myData = 'aaa' -> myData_insensitive = 'AAA' DocC: -> myData = 'BBB' -> myData_insensitive = 'BBB' DocD: -> myData = 'bbb' -> myData_insensitive = 'BBB' 

Now you can order myData_insensitive , but display myData

+7
source

You can also do this manually after receiving the results:

 docArray.sort((a, b) => { if (a.myData.toLowerCase() < b.myData.toLowerCase()) { return -1; } if (a.myData.toLowerCase() > b.myData.toLowerCase()) { return 1; } return 0; }); 
0
source

Source: https://habr.com/ru/post/1274501/


All Articles