1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
| unit StringridSort;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls;
type TMy2Compare=Function(Const R1c1,R2c1,R1c2,R2c2:String):Boolean;
TStringGridX = class(TCustomGrid) public procedure MoveRow(FromIndex, ToIndex: Longint); procedure MoveColumn(FromIndex, ToIndex: Longint); procedure DeleteRow(ARow: Longint); procedure DeleteColumn(ACol: Longint); end;
TForm1 = class(TForm) StringGrid1: TStringGrid; Button1: TButton; procedure Button1Click(Sender: TObject); private public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TStringGridX.DeleteColumn(ACol: Integer); begin inherited; end;
procedure TStringGridX.DeleteRow(ARow: Integer); begin inherited; end;
procedure TStringGridX.MoveColumn(FromIndex, ToIndex: Integer); begin inherited; end;
procedure TStringGridX.MoveRow(FromIndex, ToIndex: Integer); begin inherited; end;
Function Compare2RowsIamSureTheyAreNumericDesc(Const R1c1,R2c1,R1c2,R2c2:String):Boolean; begin if StrToInt(R1c1)=StrToInt(R2c1) then Result := StrToInt(R1c2)<StrToInt(R2c2)
else Result:=StrToInt(R1c1)<StrToInt(R2c1); end;
Function Compare2RowsIamSureTheyAreNumericAsc(Const R1c1,R2c1,R1c2,R2c2:String):Boolean; begin if StrToInt(R1c1)=StrToInt(R2c1) then Result := StrToInt(R1c2)>StrToInt(R2c2)
else Result:=StrToInt(R1c1)>StrToInt(R2c1); end;
procedure QuickSortSG2Rows(a:TStringGrid;Column1,Column2:Integer; lo, hi: integer;Compare:TMy2Compare); var i, j, h: integer; s1,s2:String; begin i:=lo; j:=hi; s1:=a.Cells[Column1,(lo+hi) div 2]; s2:=a.Cells[Column2,(lo+hi) div 2]; while i<=j do begin While (i<=j) and Compare(s1,a.Cells[Column1,i],s2,a.Cells[Column2,i]) do inc(i); While (i<=j) and Compare(a.Cells[Column1,j],s1,a.Cells[Column2,j],s2) do dec(j); if (i<=j) then begin TStringGridX(a).MoveRow (i,j); if j-i>1 then TStringGridX(a).MoveRow (j-1,i); inc(i); dec(j); end; end;
if lo<j then QuickSortSG2Rows(a, Column1,Column2, lo, j, Compare); if i<hi then QuickSortSG2Rows(a, Column1,Column2, i, hi, Compare); end;
procedure TForm1.Button1Click(Sender: TObject); begin QuickSortSG2Rows(StringGrid1,1,2, 1, StringGrid1.RowCount - 1 ,Compare2RowsIamSureTheyAreNumericAsc) end;
end. |