排序TListView列 [英] Sorting TListView Columns
问题描述
我有一个包含4列的TListview(这些都是字符串),但是,我们将数据存储在其中:
-
Caption
:任何字符串 -
SubItems [0] code>:integer,eg
'5016'
-
SubItems [1]
:日期,例如'03 / 22/13'
-
Subitems [2] code>:任何字符串
当用户单击冷列标题时,我使用以下代码进行排序p>
我正在看这篇文章如何在Tlistview中根据子项[x]进行排序,但是我不知道如何考虑不同的列类型。
程序TfrmFind.lvwTagsColumnClick(发件人:TObject;列:TListColumn);
begin
ColumnToSort:= Column.Index;
(发件人为TCustomListView).AlphaSort;
结束
procedure TfrmFind.lvwTagsCompare(Sender:TObject; Item1,Item2:TListItem;
Data:Integer; var Compare:Integer);
var
ix:整数;
begin
如果ColumnToSort = 0然后
比较:= CompareText(Item1.Caption,Item2.Caption)
else begin
ix:= ColumnToSort - 1;
比较:= CompareText(Item1.SubItems [ix],Item2.SubItems [ix]);
结束
结束
我如何考虑整数和日期列,以便它们不会被排序成字符串? / p>
谢谢
如果您有两个包含整数的字符串,希望作为整数进行比较,然后将它们从文本转换为整数,并进行数字比较。
function CompareTextAsInteger(const s1,s2:string ): 整数;
begin
结果:= CompareValue(StrToInt(s1),StrToInt(s2));
结束
同样的日期。将它们从文本转换为数值,例如 TDateTime
值。然后比较数字。
function CompareTextAsDateTime(const s1,s2:string):Integer;
begin
结果:= CompareDateTime(StrToDateTime(s1),StrToDateTime(s2));
结束
正是如何实现后一种功能取决于您希望如何从文本转换为数字表示的日期/时间。
I have a TListview with 4 columns (which are all strings of course) however, I store data in them as such:
Caption
: any stringSubItems[0]
: integer, e.g.'5016'
SubItems[1]
: date, e.g.'03/22/13'
Subitems[2]
: any string
I use the following code to sort when user clicks a coolumn header
I was looking at this post "how to sort in Tlistview based on subitem[x]" but I can't figure out how to take the different column types into consideration.
procedure TfrmFind.lvwTagsColumnClick(Sender: TObject; Column: TListColumn);
begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
procedure TfrmFind.lvwTagsCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;
end;
How can I take into consideration of the Integer and Date columns so they are not sorted as strings?
thankx
If you have two strings that contain integers and you wish to compare as integers then convert them from text to integer, and compare numerically.
function CompareTextAsInteger(const s1, s2: string): Integer;
begin
Result := CompareValue(StrToInt(s1), StrToInt(s2));
end;
And similarly for dates. Convert them from text to numeric values, for example TDateTime
values. And then compare numerically.
function CompareTextAsDateTime(const s1, s2: string): Integer;
begin
Result := CompareDateTime(StrToDateTime(s1), StrToDateTime(s2));
end;
Exactly how you implement this latter function depends on how you want to convert from text to numeric representation of date/time.
这篇关于排序TListView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!