Trier les colonnes d'un StringGrid

type
TMoveSG = class(TCustomGrid);

procedure SortGridByCols(Grid: TStringGrid; ColOrder: array of integer);
var
i, j: integer;
Sorted: boolean;

function Sort(Row1, Row2: integer): integer;
var
C: integer;
begin
C := 0;
result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]);

if result = 0 then begin
Inc(C);
while (C <= High(ColOrder)) and (result = 0) do begin
result := AnsiCompareStr(Grid.Cols[ColOrder[C]][Row1], Grid.Cols[ColOrder[C]][Row2]);
Inc(C);
end;
end;
end;

begin
if SizeOf(ColOrder) div SizeOf(i) <> Grid.ColCount then exit;
for i := 0 to High(ColOrder) do
if (ColOrder[i] < 0) or (ColOrder[i] >= Grid.ColCount) then exit;

j := 0;
Sorted := false;
repeat
inc(j);
with Grid do
for i := 0 to RowCount - 2 do
if Sort(i, i + 1) > 0 then begin
TMoveSG(Grid).MoveRow(i + 1, i);
Sorted := false;
end;
until Sorted or (j = 1000);
Grid.Repaint;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SortGridByCols(StringGrid1, [1, 2, 0, 3, 4]);
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour16/11/2001
VisualisationVu 960 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur