sortingdelphicomparatorvarianttcxgrid

Does Delphi String-variant comparison use loInvariantLocale? How to locally enable loUserLocale instead?


My problem arises from DevExpress TcxGrid default comparison (used for the sorting of data), which boils down to the code (implemented in cxVariants.pas):

  if VarIsEmpty(V1) then
    if VarIsEmpty(V2) then
      Result := 0
    else
      Result := -1
  else
    if VarIsEmpty(V2) then
      Result := 1
    else
      if V1 = V2 then
        Result := 0
      else
        if V1 < V2 then
          Result := -1
        else
          Result := 1;

My tests assure that V1 < V2 works as CompareStr(V1, V2, loInvariantLocale), but usually one would like that V1 < V2 should work as CompareStr(V1, V2, loUserLocale).

And this is also reflected in the sorting results of cxGrid: usually Eastern European languages expect that diacritical characters follow immediately their original characters, but loInvariantLocal puts all the diacritical characters at the end of all non-diacritical characters. One expects orders aā .. zž, but cxGrid sorts like a .. z, ā .. ž.

So: is it possible to ask some code fragment to use loUserLocale for variant comparison?

Of course, I will just override cxGrid.cxGridDBTableView.Controller.OnCompare(), but I am a bit uneasy about the general situation whether I can or I can not affect variant comprison.


Solution

  • The default is to compare the ordinal values of the characters. To compare based on the locale enable the DataController's option dcoAnsiSort (cxGrid1DBTableView1.DataController.Options.dcoAnsiSort).

    dcoAnsiSort

    If active, a grid control uses Windows locale settings to sort records. This option is useful when grid cells contain native language text.

    Otherwise, strings are compared based on the 8-bit ordinal value of each character.

    The docs are a bit dated - for wide characters the 16-bit ordinal values are compared. (Ref:TcxDataControllerOption Enum)