mplynch Posted January 8, 2015 Report Share Posted January 8, 2015 In the attached VI, I have an editor of sorts which permits the modification of a list of strings. There is a listbox which displays the list in sorted order, and a text box used for modifying the currently selected item. I'm struggling with determining the best approach to maintain selection in the listbox after sorting takes place. The only solution I've really come up with is to do a linear search for the new value in the listbox's ItemNames array and set the listbox's value accordingly, but I'd like to be able to allow duplicate values. In that case, the search obviously creates some odd behavior. Does anyone have any suggestions on how to create a sorted listbox that maintains selection when trying to edit one of the items? Thanks for your help! Sorted Listbox Example.vi Link to comment
Darin Posted January 8, 2015 Report Share Posted January 8, 2015 I am running LV12 at the moment so I can not see your VI, but you described it well enough I think. I would suggest a keyed sort of your ItemNames. Use a loop to bundle the item name and its current index into a cluster array (in that order). Sort this cluster array. Create a single cluster consisting of the new name and its old index (the listbox value I presume) and search for this element inside the sorted cluster array. Use the index of the match as the new value of your listbox and update the item names with the sorted strings. For the size of arrays likely to occur in a typical listbox I would sort the strings separately instead of trying to unbundle in a loop. Link to comment
mplynch Posted January 8, 2015 Author Report Share Posted January 8, 2015 Thanks for the suggestion! I wish I knew what the Big-O was for the Sort Array VI, as the only other worry I have is what will happen when the list grows. I did some quick profiling with it, and it seems like it's O(n^2). I guess I'd better just limit the uses of this sort of editor to instances in which the listbox size will remain relatively small. Attached is a working example, both in LV2013 and 2009 for posterity... Sorted Listbox Example.vi Sorted Listbox Example LV2009.vi Link to comment
GregSands Posted January 8, 2015 Report Share Posted January 8, 2015 (edited) I would use the OpenG Sort Array function, or similar, like this: Edited January 8, 2015 by GregSands Link to comment
mplynch Posted January 8, 2015 Author Report Share Posted January 8, 2015 That certainly cleans things up a bit. Great suggestion! Link to comment
GregSands Posted January 9, 2015 Report Share Posted January 9, 2015 (edited) Just thought of a couple more ideas for you which use less memory and may be quicker (I haven't benchmarked them): Instead of returning the sorted indices array and searching it, you can just find the location as you create the string array, like this: If you know that the original list is sorted, then there's no need to sort the whole array again - it's easier to delete the current element from the array, then determine directly where to insert it: This also lends itself to easily adding new elements (just eliminate "Delete From Array"). (I fluked the string comparison - hadn't realised it would work, but I guess it just treats the string as a byte array). Edited January 9, 2015 by GregSands Link to comment
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now