2013-03-19 82 views
1

是的TStringList CustomSort方法是posssible使用的名稱從名稱/值對的名稱/值對

目前我使用的是的TStringList排序在每個POS一個值來使用customSort在TStringList中。我現在需要用這個值添加額外的數據,併爲此我現在使用的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); 

有沒有修改這個,以便它,並據此排序方式在名稱值對的名稱?

或者,有沒有另一種方法?

回答

7
function MyCompare(List: TStringList; Index1, Index2: Integer): Integer; 
begin 
    Result := StrCmpLogicalW(PWideChar(List.Names[Index1]), PWideChar(List.Names[Index2])); 
end; 

但實際上,我認爲你應該工作爲好,因爲字符串本身以名稱無論如何,由整個字符串,因此排序按名稱排序隱含它。

3

爲了解決這個問題,你使用它的文件中是這樣描述的Names索引屬性:

表示是名稱 - 值對字符串的名稱部分。

當TStrings對象的字符串列表包含 是名稱 - 值對的字符串時,請讀取名稱以訪問字符串的名稱部分。 名稱是Index中字符串的名稱部分,其中0是第一個 字符串,1是第二個字符串,依此類推。如果該字符串不是 名稱 - 值對,則Names包含一個空字符串。

因此,而不是List[Index1]你只需要使用List.Names[Index1]。因此,你的比較函數變爲:

function MyCompare(List: TStringList; Index1, Index2: Integer): Integer; 
begin 
    Result := StrCmpLogicalW(
    PChar(List.Names[Index1]), 
    PChar(List.Names[Index2]) 
); 
end;