delphi 对TStringList中的数值进行排序  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 对TStringList中的数值进行排序


//排序函数,作为参数传递给CustomSort方法.降序,如果要升序的话将-1和1调个个就行了,呵呵。

function NumberSort(List: TStringList; Index1, Index2: Integer): Integer;

var

Value1,Value2:Integer;

begin

Value1:=StrToInt(List[Index1]);

Value2:=StrToInt(List[Index2]);

if Value1>Value2 then

Result:=-1

else if Value1<Value2 then

Result:=1

else

Result:=0;

end;


//填充数据并排序

procedure TForm1.Button1Click(Sender: TObject);

var

SL:TStringList;

i: Integer;

begin

SL:=TStringList.Create;

try

for i:=0 to 100 do

begin

SL.Add(IntToStr(Random(1000)));

end;

SL.Add('测试错误');

Listbox1.Items.Assign(SL); //排序前

SL.CustomSort(NumBerSort); //排序

ListBox2.Items.Assign(SL); //排序后

finally

SL.Free;

end;

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.045615911483765 seconds