你自己修改一下吧
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure DBGrid1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
SortBool: Boolean = True;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
i: Integer;
begin
Screen.Cursor := crHourGlass;
for i := 0 to (Sender as TDBGrid).Columns.Count - 1 do
if (Copy((Sender as TDBGrid).Columns.FieldName, 1, 3) = '▲ ') or (Copy((Sender as TDBGrid).Columns.FieldName, 1, 3) = '▼ ') then
(Sender as TDBGrid).Columns.FieldName := Copy((Sender as TDBGrid).Columns.FieldName, 4, Length((Sender as TDBGrid).Columns.FieldName) - 3);
case (Sender as TDBGrid).Columns.Index of
0,1,2,3,4,5,6,7,11,12,13,14,20,21,23,24,25,26,27,28,30,31:
if SortBool then
begin
if (Sender as TDBGrid).SelectedRows <> nil then
begin
QuickSortStringAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortStringAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortStringDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortStringDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
9,10,15,22:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortInt64Ascend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortInt64Ascend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortInt64Descend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortInt64Descend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
8,16,29:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortExtendedAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortExtendedAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortExtendedDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortExtendedDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
17,18,19:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortDateTimeAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortDateTimeAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortDateTimeDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortDateTimeDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
end;
Screen.Cursor := crDefault;
end;
end.
procedure ListItemExchange(ListItemA, ListItemB: TListItem; Count: Integer);
var
Temp: String;
i: Integer;
begin
for i := 0 to Count - 1 do
begin
if i = 0 then
begin
Temp := ListItemA.Caption;
ListItemA.Caption := ListItemB.Caption;
ListItemB.Caption := Temp;
end
else
begin
Temp := ListItemA.SubItems[i - 1];
ListItemA.SubItems[i - 1] := ListItemB.SubItems[i - 1];
ListItemB.SubItems[i - 1] := Temp;
end;
end;
end;
Procedure QuickSortStringAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: String;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].Caption;
repeat
while CompareText(ListView.Items.Item[Lo].Caption, Mid) < 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].Caption, Mid) > 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1];
repeat
while CompareText(ListView.Items.Item[Lo].SubItems[Column - 1], Mid) < 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].SubItems[Column - 1], Mid) > 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortStringDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: String;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].Caption;
repeat
while CompareText(ListView.Items.Item[Lo].Caption, Mid) > 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].Caption, Mid) < 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1];
repeat
while CompareText(ListView.Items.Item[Lo].SubItems[Column - 1], Mid) > 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].SubItems[Column - 1], Mid) < 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortInt64Ascend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Int64;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].Caption <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortInt64Descend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Int64;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].Caption <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortExtendedAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Extended;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption = '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortExtendedDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Extended;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
procedure QuickSortDateTimeAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: TDateTime;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
Procedure QuickSortDateTimeDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi : Integer;
Mid: TDateTime;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end;
end;