[fpc-devel] TStringList.CustomSort

Chris Cheney C.J.Cheney at ucs.cam.ac.uk
Tue Nov 7 10:12:13 CET 2006

> Does anyone use the CustomSort facility of TStringList ? I am a bit
> troubled because it's broken by design it seems. You can sort the list
> with it, but searching/adding will not work properly (or be linear). I
> want to have a callback that gives you two strings to compare, instead
> of two indexes which you have to fetch anyway from the list, first.
> Thoughts ?

I am not sure I understand. Do you mean the last sentence to say "I want 
to have a callback having the parameters as the two strings to compare, 
instead of two indexes, as you have to fetch the strings from the list 

Stating the obvious: but the programmer might wish to sort by the objects 
or by some combination of the strings and the objects (or even by entries 
in an external array, unrelated to the StringList) - the indexes are 
therefore needed in the general case.

As to the implementation being broken by design, note from Delphi help:	
"You must explicitly call the CustomSort method. Setting the Sorted 
property only sorts strings using ANSI order, as implemented in the Sort 
method." Whether that means it is broken or not depends on your point of 

Of course, the efficient way to build a sorted list is to set Sorted to 
False and to sort the list after all the items have been added.

Mr C.J. Cheney, Head of the Network Division
University of Cambridge Computing Service
New Museums Site, Pembroke Street
Cambridge CB2 3QH
tel: 01223 334428

More information about the fpc-devel mailing list