2012-06-30 115 views
2

我有一个巨大的用户列表,每个用户都有它的ID,但它的ID号被搞砸所以如果有人能告诉我我该怎么排序号的用户,每个值都有这种形式如何按第一个数字对列表进行排序?

1:Stackoverflow 
or 
145000:Google 

如果我这样做手工,我想我会失去我的心,因为tehere超过70万个records.Thanks您的时间和帮助....

+0

看到http://stackoverflow.com/questions/5134712/how-to-get-the-sort-order-in-delphi-as-in-windows-explorer – kludg

回答

10

提取这样的数字:

function ID(const str: string): Integer; 
var 
    p: Integer; 
begin 
    p := Pos(':', str); 
    if p=0 then 
    raise Exception.CreateFmt('Invalid string format: %s', [str]); 
    Result := StrToInt(Copy(str, 1, p-1)); 
end; 

一旦你可以提取该ID作为一个整数,然后可以写入ac ompare函数。像这样:

function CompareIDs(List: TStringList; Index1, Index2: Integer): Integer; 
begin 
    Result := CompareValue(
    ID(List[Index1]), 
    ID(List[Index2]) 
); 
end; 

CompareValue是一个RTL函数,返回-1,0或1取决于两个操作数的相对值。

将这些积木填入TStringList.CustomSort并完成工作。

MyStringList.CustomSort(CompareIDs); 
+0

结果= ValueCompare(ID1,ID2 ); – Marck

+0

@Marck你的意思是'CompareValue'我认为。我同意最好使用内置的例程,但我不知道它。我会更新答案。 –

相关问题