Jump to content
amit

How to Sort TStringList ?

Recommended Posts

I have strings in the TStringlist as follows

 

ABC10

ABC1

ABC2

ABC21

 

I sort it using TStringlist.sort then I got the sorted result as

 

ABC1

ABC10

ABC2

ABC21

 

But I need to have the results after sort as follows

 

ABC1

ABC2

ABC10

ABC21

 

Is it possible to do that?

Share this post


Link to post
16 hours ago, amit said:

Is it possible to do that?

Yes, simply use TStringList.CustomSort() to sort the strings however you want, eg:

function MySortFunc(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToInt(Copy(List[Index2], 4, MaxInt)) - StrToInt(Copy(List[Index1], 4, MaxInt));
end;
...
var SL: TStringList;
SL := TStringList.Create;
...
SL.Add('ABC10');
SL.Add('ABC1');
SL.Add('ABC2');
SL.Add('ABC21');
SL.CustomSort(@MySortFunc);
...
SL.Free;

 

Edited by Remy Lebeau
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×