delphi 单击单选让LISTVIEW按时间排序?如图

图片说明

让 2017-12-18 在最上面
我现在的代码是这样的:

  lv1.Clear;
  With qry1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from FJR order by fjr_dysj');
    Open;
  end;
//  qry1.First;
  while not qry1.eof do begin
    Newitem(qry1ID.Value, qry1fjr_name.Value, qry1fjr_xxdz.Value, qry1fjr_dh.Value, qry1fjr_yb.Value, qry1fjr_dysj.Value, qry1fjr_bz.Value);
    qry1.Next;    //显示窗体时候的代码LISTVIEW

  end;
  qry1.Close; 

//ListView排序的回调函数,默认的是快速排序法,也可以自己在这里做算法

function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var
txt1, txt2: string;
begin
if ParamSort <> 0 then
begin
try
txt1 := Item1.SubItems.Strings[ParamSort - 1];
txt2 := Item2.SubItems.Strings[ParamSort - 1];
if m_bSort then
begin
Result := CompareText(txt1, txt2);
end
else
begin
Result := -CompareText(txt1, txt2);
end;
except
end;

end
else
begin
if m_bSort then
begin
Result := CompareText(Item1.Caption, Item2.Caption);
end
else
begin
Result := -CompareText(Item1.Caption, Item2.Caption);
end;
end;
end;

//ListView.OnColumnClick事件
procedure TForm1.lv1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
lv1.CustomSort(@CustomSortProc, Column.Index);
//m_bSort := not m_bSort; 自动升序或降序,点一下升序,再点一下降序
end;

搞定!

参考:http://www.delphitop.com/html/kongjian/3066.html
http://blog.sina.com.cn/s/blog_4df2251d010008pv.html