Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 为什么我的TStringList没有分类?_Delphi_Sorting - Fatal编程技术网

Delphi 为什么我的TStringList没有分类?

Delphi 为什么我的TStringList没有分类?,delphi,sorting,Delphi,Sorting,我有一个自定义排序的TStringList Items.CustomSort(@CompareWords); 。。。使用此比较功能: function CompareWords(List: TStringList; Index1, Index2: Integer): Integer; begin Result := StrIComp(PWideChar(List[Index1]), PWideChar(List[Index2])); end; 但是在注意到我的代码存在一些问题后,我创建了

我有一个自定义排序的
TStringList

Items.CustomSort(@CompareWords);
。。。使用此比较功能:

function CompareWords(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrIComp(PWideChar(List[Index1]), PWideChar(List[Index2]));
end;
但是在注意到我的代码存在一些问题后,我创建了这个小检查,它希望列表按照
StrIComp
includes的顺序进行排序

for i := 1 to Items.Count - 1 do
begin
  Assert(StrIComp(PWideChar(Items[i-1]), PWideChar(Items[i])) <= 0);
end;
对于i:=1到项。计数-1 do
开始

断言(StrIComp(PWideChar(Items[i-1]),PWideChar(Items[i]))您可能有Items.Sorted=True。

您确定了:)Items.Sorted为True,显然导致了一些奇怪的行为。我还更新了我的问题,以删除您指出的代码错误。你能不能把你的答案改一下,只指向排序后的属性?那我就接受了。您使用的是哪个Delphi版本?(IOW:项目[I]到PWideChar的转换是否有效?)。为什么使用StrIComp而不是CompareText?3.调用Sort的结果是否正常?是的,类型转换是正确的。我使用字符串列表来准备一个二进制文件,稍后将使用StrLIComp对其进行二进制搜索。当两个部分使用相同的比较方法时,我感觉好多了。在调用
CustomSort
时,不要在函数前面使用
@
。它可以掩盖比较函数声明中的错误。(在这种情况下,您可以)另外,请键入cast to
PChar
,而不是
PWideChar
。仅仅因为他们对你来说是同一类型并不意味着他们永远都是同一类型
TStringList
保存通用字符串,因此键入cast到generic
PChar
。(即使您从未使用过不同的Delphi版本,到这里来复制和粘贴您的代码的其他人也可能会这样做,因此最好不要在不需要时使用版本敏感的代码。)