Query with order, places accented value at the end

105 views Asked by At

In my flutter project, I use box.query(myquery)..order(Kategoriak_.cikkcsoportnev).build(), but my Áramfejlesztők, szivattyúk category is at the end of the list.

How can I order it correctly, so Á would come after A, not at the end?

enter image description here

I am using objectbox: ^1.7.0

2

There are 2 answers

0
Ebadta On BEST ANSWER

I ended up implementing a short function using a Hungarian string array. It doesn't handle double and triple letters, but it's fine in most cases.

I didn't want to double my fileds, because I have a big database.

My solution:

  const List<String> hunABC = [
    "A", "a", "Á", "á", "B", "b", "C", "c", "D", "d",
    "E", "e", "É", "é", "F", "f", "G", "g", "H", "h", "I","i", "Í", "í", 
    "J", "j", "K", "k", "L", "l", 
    "M", "m", "N", "n", "O", "o", "Ó", "ó", "Ö", "ö", "Ő", "ő",
    "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", 
    "Ú", "ú", "Ü", "ü", "Ű", "ű", 
    "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z"
  ];
  

  int hunCompare(int num, int i, String input, int charNum) {
    if (num == -1)
    {
      if (input[charNum] == hunABC[i][0]) {
        return i;
      }

      return -1;
    }
    
    return num;  
  }

And my Comparable implementation for my class:

  @override
  int compareTo(Kategoriak other) {
    int otherNum = -1;
    int inputNum = -1;
    int charNum = 0;

    for (int i = 0; i < hunABC.length; i++)
    {
      inputNum = hunCompare(inputNum, i, cikkcsoportnev, charNum);
      otherNum = hunCompare(otherNum, i, other.cikkcsoportnev, charNum);

      if (i == hunABC.length) {
        if (inputNum == -1) inputNum = hunABC.length+1;
        if (otherNum == -1) otherNum = hunABC.length+1;
      }

      if (inputNum > -1 && otherNum > -1) {
        if (inputNum < otherNum) return -1;
        if (inputNum > otherNum) return 1;
        if (inputNum == otherNum) {
          if (cikkcsoportnev.length > charNum+1
          && other.cikkcsoportnev.length > charNum+1) {
            charNum += 1;
            inputNum = -1;
            otherNum = -1;
            i = -1;
          } else { return 0; }
        }
      }
    }

    return 0;
  }
5
Peter Koltai On

I am also from Hungary :-)

I faced the same problem multiple times. My solution is that I store and update all search / order field values in the database without accents and converted to lowercase.

When I need to search and / or order, I use these duplicated fields and use the original fields to output the values.

I don't know objectbox, but in Firestore I use triggers to manage these duplicated fields when new documents are added or this field is modified.

As for removal of accents, I use this dart package which according to the sample in readme (removeDiacritics('árvíztűrő tükörfúrógép')) was more than likely written by someone who speaks Hungarian.