سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top