Search code examples
delphiexceptionsortedlistsortingtstringlist

Why does Delphi's TStringList.InsertObject() method thrown an Exception if the list is Sorted?


In Delphi 6 if you try to insert an object into a TStringList that is sorted (Sorted = true) an Exception is thrown warning you that InsertObject() is not allowed on a sorted list. I could understand this if calling InsertObject() necessarily meant destroying the Sorted order of the list. But given that the TStringList.Find() method:

function TStringList.Find(const S: string; var Index: Integer): Boolean;

returns an index telling you exactly what the insertion index should be for a given string if it were added to the list, calling InsertObject() with that index should leave the sorted list still in sorted order after the operation. I've examined the Delphi source for TStringList and it seems to bear out my assertion.

For now I'm just creating a new sub-class for TStringList that overrides InsertObject() and does not throw an Exception if InsertObject() is called on sorted list, but I want to make sure there isn't some hidden danger that I'm just not seeing.

-- roschler


Solution

  • You should just call AddObject instead on a sorted list.

    If InsertObject checked for the 'correct' index on sorted lists, then you'd have a testing nightmare: Under some circumstances, your code would appear to work, but would suddenly start throwing exceptions if the input data changed. Or, if InsertObject ignored the Index parameter, then its behaviour would be wildly non-intuitive.

    It's much better for InsertObject to always throw if the list is sorted.