looking for a smart approach to "sort" an array

Jim Lambert jiml at netrin.com
Sat May 6 13:10:11 EDT 2017


> I have a list field of words and a correspondent array with the words of the
> field as the keys plus some data per key. I can store the array in a file,
> read it later from file and rebuild the list of words from the keys of the
> array. Up to now, I had this list of words alphabetically sorted. So it was
> easy, when reloading the array to sort the list always alphabetically to
> refresh the visible list in the field.
> The user can also create a custom sequence of the words in the field by drag
> and drop the lines in individual order. Now I am looking for a smart
> approach to keep the same sort order in the correspondent array. I need the
> custom sort order of the words in case I reload the array later from file to
> get the same sequence of words as the user has sorted them, after extracting
> the words from the array. 

As much as I like arrays, in this case you might consider an ‘old school’ approach.
This approach will only work if the stack is writable and that 'plus some data per key’ is just text.
And the approach is - keep everything in the scrolling list field - including the OtherData. No arrays, no writing files.

Set the tabstops to a number greater than the width of the field.
Set the itemdelimter to tab.
Then put the OtherData of each line into the second item of each line.

As long as there’s no horizontal scrollbar on the field the user will never see the OtherData.
Getting item 2 of the hilitedline will always give you the corresponding OtherData for the word the user has selected.

Your drag and drop routines should still work. Just remember to save the stack each time the user rearranges the line order.

Jim Lambert

More information about the use-livecode mailing list