hier ist der Ausschnitt aus einem Programm, das ich ketztes Jahr mal geschrieben habe:
Code: Alles auswählen
for i := 1 to gridzeilen - 1 do
begin
for j := i + 1 to gridzeilen do
begin
tauschen := false;
if ARow = 1 then
begin
case ACol of
0: begin
if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] <> '') then
begin
if StringGrid2.Cells[ACol, i][1] > StringGrid2.Cells[ACol, j][1] then tauschen := true;
if StringGrid2.Cells[ACol, i][1] = StringGrid2.Cells[ACol, j][1] then
begin
if StrToInt(Copy(StringGrid2.Cells[ACol, i], 2, Length(StringGrid2.Cells[ACol, i]) - 1)) > StrToInt(Copy(StringGrid2.Cells[ACol, j], 2, Length(StringGrid2.Cells[ACol, j]) - 1)) then tauschen := true;
end;
end
else
begin
if (StringGrid2.Cells[ACol, i] = '') and (StringGrid2.Cells[ACol, j] <> '') then tauschen := true;
//if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] = '') then tauschen := true;
end;
end;
5, 9, 10, 11, 12: if StringGrid2.Cells[ACol, i] > StringGrid2.Cells[ACol, j] then tauschen := true;
1: if StrToDate(StringGrid2.Cells[ACol, i]) > StrToDate(StringGrid2.Cells[ACol, j]) then tauschen := true;
2: begin
if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] <> '')then
begin
if StrToDate(StringGrid2.Cells[ACol, i]) > StrToDate(StringGrid2.Cells[ACol, j]) then tauschen := true;
end
else
begin
if (StringGrid2.Cells[ACol, i] = '') and (StringGrid2.Cells[ACol, j] <> '') then tauschen := true;
end;
end;
3,4,6: if StrToInt(StringGrid2.Cells[ACol, i]) > StrToInt(StringGrid2.Cells[ACol, j]) then tauschen := true;
7, 8: if StrToFloat(StringGrid2.Cells[ACol, i]) > StrToFloat(StringGrid2.Cells[ACol, j]) then tauschen := true;
end;
end
else
begin
case ACol of
0: begin
if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] <> '') then
begin
if StringGrid2.Cells[ACol, i][1] < StringGrid2.Cells[ACol, j][1] then tauschen := true;
if StringGrid2.Cells[ACol, i][1] = StringGrid2.Cells[ACol, j][1] then
begin
if StrToInt(Copy(StringGrid2.Cells[ACol, i], 2, Length(StringGrid2.Cells[ACol, i]) - 1)) < StrToInt(Copy(StringGrid2.Cells[ACol, j], 2, Length(StringGrid2.Cells[ACol, j]) - 1)) then tauschen := true;
end;
end
else
begin
if (StringGrid2.Cells[ACol, i] = '') and (StringGrid2.Cells[ACol, j] <> '') then tauschen := true;
//if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] = '') then tauschen := true;
end;
end;
5, 9, 10, 11, 12: if StringGrid2.Cells[ACol, i] < StringGrid2.Cells[ACol, j] then tauschen := true;
1: if StrToDate(StringGrid2.Cells[ACol, i]) < StrToDate(StringGrid2.Cells[ACol, j]) then tauschen := true;
2: begin
if (StringGrid2.Cells[ACol, i] <> '') and (StringGrid2.Cells[ACol, j] <> '')then
begin
if StrToDate(StringGrid2.Cells[ACol, i]) < StrToDate(StringGrid2.Cells[ACol, j]) then tauschen := true;
end
else
begin
if (StringGrid2.Cells[ACol, i] = '') and (StringGrid2.Cells[ACol, j] <> '') then tauschen := true;
end;
end;
3, 4, 6: if StrToInt(StringGrid2.Cells[ACol, i]) < StrToInt(StringGrid2.Cells[ACol, j]) then tauschen := true;
7, 8: if StrToFloat(StringGrid2.Cells[ACol, i]) < StrToFloat(StringGrid2.Cells[ACol, j]) then tauschen := true;
end;
end;
if tauschen then
begin //ShowMessage('Austausch Zeilen ' + IntToStr(i) + ' <-> ' + IntToStr(j));
for spalte := 0 to 12 do
begin
dummy := StringGrid2.Cells[spalte, i];
StringGrid2.Cells[spalte, i] := StringGrid2.Cells[spalte, j];
//Sleep(1000);
StringGrid2.Cells[spalte, j] := dummy;
//Sleep(1000);
end;
end;
end;
end;
In den Spalten 0 bis 12 befinden sich Daten unterschiedlicher Datentypen. Datum, Zahlen, Texte, Beträge, oder auch leere Felder.
Der Algrithmus sortiert den StringGrid auf- bzw. absteigend. Beim Algorithmus handelt es sich um "Linear-Sort", also das langsamste Verfahren, das es gibt. Bei den heutigen Prozessoren und den Datenmengen meines Programms reicht mir dieses.