Search code examples
pascalfreepascalfpc

Can TStringList.Sort return sorted indexes?


Using TStringList.Sort to sort a collection of strings in free pascal, I need to remember the initial order. Is there a possibility to return the sorted indexes? If not, how can this be done efficiently?


Solution

  • You can use the object property to store the original index of the item.

    So you can insert your items in this way

    SL.AddObject('Item 1', TObject(SL.Count));
    SL.AddObject('Item 2', TObject(SL.Count));
    

    and retrieve the original index after sort the TStringList

       Index := Integer(SL.Objects[i]);