名称/值对上的TStringList CustomSort方法

Jak*_*ays 1 delphi delphi-2010

是否可以使用名称/值对中的名称在TStringList上使用customSort

我当前正在使用TStringList对每个pos中的一个值进行排序.我现在需要使用此值添加其他数据,因此我现在使用TStringList作为名称/值

我目前的CompareSort是:

function StrCmpLogicalW(sz1, sz2: PWideChar): Integer; stdcall;
  external 'shlwapi.dll' name 'StrCmpLogicalW';


function MyCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrCmpLogicalW(PWideChar(List[Index1]), PWideChar(List[Index2]));
end;
Usage:
  StringList.CustomSort(MyCompare);
Run Code Online (Sandbox Code Playgroud)

有没有办法修改它,以便它根据名称值对的名称进行排序?

或者,还有另一种方式吗?

Gol*_*rol 7

function MyCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrCmpLogicalW(PWideChar(List.Names[Index1]), PWideChar(List.Names[Index2]));
end;
Run Code Online (Sandbox Code Playgroud)

但实际上,我认为你的应该也可以工作,因为字符串本身始终以名称开头,因此按整个字符串排序会隐式按名称排序.