[英]How can I search faster for name/value pairs in a Delphi TStringList?
[英]Custom Sort a Delphi TStringList name/value pairs as integers
我有一个名称/值对的TStringList。 这些名称都是9个整数值,当然是字符串),值都是字符串(以逗号分隔)。
例如
5016=Catch the Fish!,honeyman,0
30686=Ozarktree1 Goes to town,ozarktreel,0
。 。 。
我想调用add例程并在TStringlist中添加新行,但之后需要一种方法对列表进行排序。
例如
Tags.Add(frmTag.edtTagNo.Text + '=' +
frmTag.edtTitle.Text + ',' +
frmTag.edtCreator.Text + ',' +
IntToStr(ord(frmTag.cbxOwned.Checked)));
Tags.Sort;
这是我尝试过的:
Tags:= TStringList.Create;
Tags.CustomSort(StringListSortComparefn);
Tags.Sorted:= True;
我的自定义排序例程:
function StringListSortComparefn(List: TStringList; Index1, Index2: Integer): Integer;
var
i1, i2 : Integer;
begin
i1 := StrToIntDef(List.Names[Index1], 0);
i2 := StrToIntDef(List.Names[Index2], 0);
Result:= CompareValue(i1, i2);
end;
但是,它似乎仍然像字符串而不是整数一样对它们进行排序。
我甚至尝试创建自己的类:
type
TXStringList = class(TStringList)
procedure Sort;override;
end;
implementation
function StringListSortComparefn(List: TStringList; Index1, Index2: Integer): Integer;
var
i1, i2 : Integer;
begin
i1 := StrToIntDef(List.Names[Index1], 0);
i2 := StrToIntDef(List.Names[Index2], 0);
Result:= CompareValue(i1, i2);
end;
procedure TXStringList.Sort;
begin
CustomSort(StringListSortComparefn);
end;
我甚至在SO上尝试了一些例子(例如,将TSTringList Names属性排序为整数而不是字符串 )
有人能告诉我我做错了什么吗? 每次,列表都按字符串排序,而不是整数。
30686=Ozarktree1 Goes to town,ozarktreel,0
5016=Catch the Fish!,honeyman,0
你可以做一个简单的整数减法:
function StringListSortComparefn(List: TStringList; Index1, Index2: Integer): Integer;
var
i1, i2 : Integer;
begin
i1 := StrToIntDef(List.Names[Index1], 0);
i2 := StrToIntDef(List.Names[Index2], 0);
Result := i1 - i2
end;
要反转排序顺序,只需在减法中反转操作数:
Result := i2 - i1;
这是一个快速,可编译的控制台示例:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
function StringListSortProc(List: TStringList; Index1, Index2: Integer): Integer;
var
i1, i2: Integer;
begin
i1 := StrToIntDef(List.Names[Index1], -1);
i2 := StrToIntDef(List.Names[Index2], -1);
Result := i1 - i2;
end;
var
SL: TStringList;
s: string;
begin
SL := TStringList.Create;
SL.Add('3456=Line 1');
SL.Add('345=Line 2');
SL.Add('123=Line 3');
SL.Add('59231=Line 4');
SL.Add('545=Line 5');
WriteLn('Before sort');
for s in SL do
WriteLn(#32#32 + s);
SL.CustomSort(StringListSortProc);
WriteLn('');
WriteLn('After sort');
for s in SL do
WriteLn(#32#32 + s);
ReadLn;
SL.Free;
end.
结果输出:
Before sort
3456=Line 1
345=Line 2
123=Line 3
59231=Line 4
545=Line 5
After sort
123=Line 3
345=Line 2
545=Line 5
3456=Line 1
59231=Line 4
问题是,您是否要求列表保持排序? 或者在添加完所有项目后,最后对它进行排序就足够了。
如果您只需要能够根据需要对列表进行排序,那么您的第一个示例几乎是正确的。 添加完项目后,您最后需要在最后调用CustomSort。
Tags := tStringList . Create;
Tags . Add ( '5016=Catch the Fish!,honeyman,0' );
Tags . Add ( '30686=Ozarktree1 Goes to town,ozarktreel,0' );
Tags.CustomSort(StringListSortComparefn);
如果您需要列表保持排序,则需要覆盖CompareStrings。
type
TXStringList = class(TStringList)
function CompareStrings(const S1, S2: string): Integer; override;
end;
function NumberOfNameValue ( const S : string ) : integer;
begin
Result := StrToIntDef(copy(S,1,pos('=',S)-1), 0);
end;
function txStringList . CompareStrings ( const S1, S2 : string ) : integer;
var
i1, i2 : Integer;
begin
i1 := NumberOfNameValue ( S1 );
i2 := NumberOfNameValue ( S2 );
Result:= CompareValue(i1, i2);
end;
begin
Tags := txstringlist . Create;
Tags . Sorted := true;
Tags . Add ( '5016=Catch the Fish!,honeyman,0' );
Tags . Add ( '30686=Ozarktree1 Goes to town,ozarktreel,0' );
// List will be correctly sorted at this point.
end;
CustomSort
命令是一次性操作。 您似乎正在使用它,就好像您正在设置属性,以便进一步排序将使用自定义比较功能,但这不是它的工作原理。 它对(新创建的,空的)列表进行一次排序。 然后,当您设置Sorted
属性时,使用默认比较重新排序列表, 并指定应使用该默认排序顺序插入列表的任何进一步添加。
当您重写Sort
方法时,您更接近解决方案,但插入到排序列表( Sorted=True
)实际上并不调用Sort
! 相反,他们执行二进制搜索正确的插入位置,然后插入。 您可以尝试重写CompareStrings
,而不是重写Sort
,
type
TXStringList = class(TStringList)
protected
function CompareStrings(const S1, S2: string): Integer; override;
end;
function TXStringList.CompareStrings(const S1, S2: string): Integer;
var
i1, i2, e1, e2: Integer;
begin
Val(S1, i1, e1);
Assert((e1 = 0) or (S1[e1] = NameValueSeparator));
Val(S2, i2, e2);
Assert((e2 = 0) or (S2[e2] = NameValueSeparator));
Result := CompareValue(i1, i2);
end;
但请注意,这会破坏IndexOf
方法。 它也可能会破坏Find
,但您可能需要这样,具体取决于您希望如何使用相同的数字键处理元素。 ( Find
是用于定位排序列表的正确插入点的内容,并且使用上面的代码,它将使用相同的键处理所有元素。)它们都像Sort
一样使用CompareStrings
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.