Pregunta

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?

¿Fue útil?

Solución

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]);

Otros consejos

A bit late, but since strings are copy on write, you can simply assign the tstringlist to a second tstringlist using

   t2.assign(t1);

This won't duplicate the strings. Due to the copy on write behaviour, tstringlist is like an index for strings.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top