@dummzeuch Your problem is not in sorting algorithm at all. Trying to optimize sorting algorithm you just solving the wrong task.
Even faster way will be to copy the data into some array, sort the array and then copy data back:
type
TDataItem = record
S: string;
O: TObject;
Checked: Boolean;
end;
TData = array of TDataItem;
var
FData: TData;
procedure TForm1.Button1Click(Sender: TObject);
var
c: Cardinal;
i: Integer;
c2: Cardinal;
begin
c := GetTickCount;
CheckListBox1.Items.BeginUpdate;
try
SetLength(FData, CheckListBox1.Items.Count); // Copy data from UI
for i := 0 to High(FData) do // control.
begin //
FData[i].S := CheckListBox1.Items[i]; //
FData[i].O := CheckListBox1.Items.Objects[i]; //
FData[i].Checked := CheckListBox1.Checked[i]; //
end; //
c2 := GetTickCount;
SortList(0, High(FData), SortCompare); // Sort data in array.
c2 := GetTickCount - c2;
for i := 0 to High(FData) do
begin
CheckListBox1.Items[i] := FData[i].S; // Copy sorted data
CheckListBox1.Items.Objects[i] := FData[i].O; // back.
CheckListBox1.Checked[i] := FData[i].Checked; //
end;
finally
CheckListBox1.Items.EndUpdate;
c := GetTickCount - c;
end;
Edit1.Text := IntToStr(c); // Whole time, including UI updates.
Edit2.Text := IntToStr(c2); // Sort only time.
end;
With 10000 items (!) this code run in:
Whole time, including UI updates - 1.5 secs (the control is slow, nothing to do with that).
Sort only time - 15 msecs.
15 msecs for sorting of 10000 items!