delphidevexpresstcxgrid

cxGrid Unicode sorting


How can you get cxGrid to preform sorting in Latin-2 (ISO-8859-2) encoding ?

I don't want the grid to be dependent on the regional settings of Windows. Problem is that I am in Slovenia but I need sorting in Croatian language. (Right now Ć,Č,Š are not sorted properly)

Can it be done ?


Solution

  • You could hook the OnCompare-event exposed by the DataController of the DBTableView for the cxGrid and implement the handler something like this:

    procedure TMyForm.cxGrid1DBTableView1DataControllerCompare( 
                    ADataController: TcxCustomDataController;
                    ARecordIndex1, ARecordIndex2, AItemIndex: Integer;
                    const V1, V2: Variant;
                    var Compare: Integer );
    var
      S1, S2       : String;
      CompareResult: Integer;
    
    begin
      S1 := V1;
      S2 := V2;
      CompareResult := CompareStringW( LANG_CROATIAN, 0, pWideChar( S1 ), -1,
                                                         pWideChar( S2 ), -1 );
      case CompareResult of
        CSTR_LESS_THAN    : Compare := -1;
        CSTR_EQUAL        : Compare := 0;
        CSTR_GREATER_THAN : Compare := 1;
      end;
    end;
    

    If S1 = 'Ć,Č,Š' and S2 = 'Č,Ć,Š' then S1 > S2, which I think is what is expected. If you switch to LOCALE_NEUTRAL you get the opposite result.

    Care should be taken when casting V1 and V2 to strings as not all columns may cast in the desired way. Dates - for example - might need special treatment.

    Also note, that CompareStringW return 0 if the function fails. A full implementation may have to deal with this.