如何按描述对TcxImageComboBox的项进行排序?

时间:2011-11-18 16:43:13

标签: delphi sorting combobox devexpress delphi-2007

我找到了一个很好的组件来实现ComboBox的Caption< - >值列表:

Is there a ComboBox that has Items like a TcxRadioGroup?

唯一的问题是:它有一个Sorted属性,但不起作用。

那么,我如何对TcxImageComboBox的项进行排序?

1 个答案:

答案 0 :(得分:3)

快速而肮脏的方法,对大多数情况应该可以正常工作:

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;