How to sort the items of a TcxImageComboBox by Des

2019-06-07 04:53发布

问题:

I've found a good component to implement a Caption<->Value List for ComboBox:

Is there a ComboBox that has Items like a TcxRadioGroup?

The only problem is: It has a Sorted property, but that doesn't work.

So, how do I sort the Items of a TcxImageComboBox?

回答1:

Quick and dirty method, should work fine for most cases:

function CompareItems(AFirst: TcxImageComboBoxItem; ASecond: TcxImageComboBoxItem): Integer;
begin
  Result := AnsiCompareText(AFirst.Description, ASecond.Description);
end;

procedure SortCxComboBoxItems(AItems: TcxImageComboBoxItems);
var
  I    : Integer;
  J    : Integer;
  PMin : Integer;
begin
  AItems.BeginUpdate;
  try
    // Selection Sort (http://en.wikipedia.org/wiki/Selection_sort)
    for I := 0 to AItems.Count - 1 do 
    begin
      PMin := I;
      for J := I + 1 to AItems.Count - 1 do 
      begin
        if CompareItems(AItems[J], AItems[PMin]) < 0 then begin
          PMin := J;
        end;
      end;
      if PMin <> I then 
      begin
        AItems[PMin].Index := I;
      end;
    end;
  finally
    AItems.EndUpdate;
  end;
end;