2011-11-18 32 views

回答

2

快速和骯髒的方法,應該工作正常在大多數情況下:

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; 
相關問題