looking for a smart approach to "sort" an array
bobsneidar at iotecdigital.com
Mon May 8 17:18:39 CEST 2017
Note also that datagrids will only display the data for columns that have been defined in the datagrid properties. You can have all sorts of data in the dgData of the datagrid that is invisible to the user. I do this all the time, storing whole database records, but displaying only the information I want the user to see.
The drag/drop rearranging is a bit trickier, but there are lessons on how to do this as well. If you need to store the data as an array, simply get the dgData of the data grid.
> On May 6, 2017, at 10:10 , Jim Lambert via use-livecode <use-livecode at lists.runrev.com> wrote:
>> 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