這是我寫的一個排序函數,你看看吧Procedure GridSort(myForm:TForm;mySGrid:TStringGrid;SortCol1,SortCol2:integer); // SortCol1 要排序的第一列 // SortCol2 要排序的第二列 var i,j,k,p,iCol:integer; sAryString,sListString:string; aryGX:array of array of string; lsbSort:TListBox; begin lsbSort:=TListBox.Create(application); lsbSort.Parent:=myForm; lsbSort.Hide; Setlength(aryGX,mySGrid.ColCount-1); for i:=0 to high(aryGX) do Setlength(aryGX[i],mySGrid.RowCount-1); for i:=1 to mySGrid.RowCount-1 do for j:=1 to mySGrid.ColCount-1 do aryGX[j-1,i-1]:=mySGrid.Cells[j,i]; with mySGrid,lsbSort do begin Items.Clear; for i:=1 to RowCount-1 do begin sListString:=Cells[SortCol1,i]+'`'+Cells[SortCol2,i]; for j:=1 to ColCount-1 do begin if (j<>SortCol1)and(j<>SortCol2) then sListString:=sListString+'`'+Cells[j,i]; end; Items.Add(sListString); end; try Sorted:=True; Except end; for i:=1 to RowCount-1 do for j:=1 to ColCount-1 do Cells[j,i]:=''; end; for i:=0 to lsbSort.Items.Count-1 do begin sListString:=lsbSort.Items.Strings[i]; for j:=0 to high(aryGX[0]) do begin sAryString:=aryGX[SortCol1-1,j]+'`'+aryGX[SortCol2-1,j]; for k:=0 to high(aryGX) do begin if (k<>SortCol1-1)and(k<>SortCol2-1) then sAryString:=sAryString+'`'+aryGX[k,j]; end; if sAryString=sListString then begin for iCol:=1 to mySGrid.ColCount-1 do mySGrid.Cells[iCol,i+1]:=aryGX[iCol-1,j]; end; end; // end of j:=0 end; for i:=0 to high(aryGX) do for j:=0 to high(aryGX[0]) do aryGX[i,j]:=''; lsbSort.Free; aryGX:=nil; end;
// SortCol1 要排序的第一列
// SortCol2 要排序的第二列
var
i,j,k,p,iCol:integer;
sAryString,sListString:string;
aryGX:array of array of string;
lsbSort:TListBox;
begin
lsbSort:=TListBox.Create(application);
lsbSort.Parent:=myForm;
lsbSort.Hide; Setlength(aryGX,mySGrid.ColCount-1);
for i:=0 to high(aryGX) do
Setlength(aryGX[i],mySGrid.RowCount-1); for i:=1 to mySGrid.RowCount-1 do
for j:=1 to mySGrid.ColCount-1 do
aryGX[j-1,i-1]:=mySGrid.Cells[j,i]; with mySGrid,lsbSort do
begin
Items.Clear;
for i:=1 to RowCount-1 do
begin
sListString:=Cells[SortCol1,i]+'`'+Cells[SortCol2,i];
for j:=1 to ColCount-1 do
begin
if (j<>SortCol1)and(j<>SortCol2) then
sListString:=sListString+'`'+Cells[j,i];
end;
Items.Add(sListString);
end;
try
Sorted:=True;
Except
end; for i:=1 to RowCount-1 do
for j:=1 to ColCount-1 do
Cells[j,i]:='';
end;
for i:=0 to lsbSort.Items.Count-1 do
begin
sListString:=lsbSort.Items.Strings[i];
for j:=0 to high(aryGX[0]) do
begin
sAryString:=aryGX[SortCol1-1,j]+'`'+aryGX[SortCol2-1,j];
for k:=0 to high(aryGX) do
begin
if (k<>SortCol1-1)and(k<>SortCol2-1) then
sAryString:=sAryString+'`'+aryGX[k,j];
end; if sAryString=sListString then
begin
for iCol:=1 to mySGrid.ColCount-1 do
mySGrid.Cells[iCol,i+1]:=aryGX[iCol-1,j];
end;
end; // end of j:=0
end;
for i:=0 to high(aryGX) do
for j:=0 to high(aryGX[0]) do
aryGX[i,j]:='';
lsbSort.Free;
aryGX:=nil;
end;