带数字和字母的Delphi CustomSort

时间:2013-03-10 02:41:49

标签: sorting delphi letters-and-numbers

Delphi使用: 2007

您好,

我有一个包含多个列的Listview。我想对一个特定的列进行排序。项目可以是数字或字母。

Example

基本上,顺序应该是以下:1-> 99,A-> Z,AA-> ZZ,AAA-> ZZZ。我在互联网上找到了一些CustomSort代码并对其进行了修改。这是结果(嗯,代码更大,但我只是把重要的东西):

function CustomSortProc(Item1, Item2: TListItem; SortColumn: Integer): Integer; stdcall;
var
 s1, s2: string;
 i1, i2: Integer;
 r1, r2: Boolean;

 function IsValidNumber(AString : string; var AInteger : Integer): Boolean;
 var
  Code: Integer;
 begin
  Val(AString, AInteger, Code);
  Result := (Code = 0);
 end;

 function CompareNumeric(AInt1, AInt2: Integer): Integer;
 begin
  if AInt1 > AInt2 then Result := 1 else
  if AInt1 = AInt2 then Result := 0 else Result := -1;
 end;

begin
 Result := 0;

 if (Item1 = nil) or (Item2 = nil) then Exit;

 r1 := IsValidNumber(s1, i1);
 r2 := IsValidNumber(s2, i2);
 Result := ord(r1 or r2);
 if Result <> 0 then Result := CompareNumeric(i1, i2) else
 begin
  Result := Length(s1) - Length(s2);
  if Result = 0 then Result := lstrcmp(PChar(s1), PChar(s2));
 end;
end;

以下是列的排序方式:

Example

换句话说,字母和数字是正确排序的。但是,我无法将所有数字放在字母之前。我确信这很简单,但我不能为我的生活弄清楚。

非常感谢。

1 个答案:

答案 0 :(得分:1)

如果一个是字符串而一个是int,则val中字符串的值为0,因此它将在之前。

if (Item1 = nil) or (Item2 = nil) then Exit;

 r1 := IsValidNumber(s1, i1);
 r2 := IsValidNumber(s2, i2);
 if (r1 and r2) then
   Result := CompareNumeric(i1, i2)
 else
   if r1 then 
     result := -1
   else
    if r2 then
      result := 1
    else
    begin
     Result := Length(s1) - Length(s2);
     if Result = 0 then 
       Result := lstrcmp(PChar(s1), PChar(s2));
    end;
end;

我想,机器上没有Dephi可以检查。