Jump to content
mplynch

Maintaining Selection In A Sorted Listbox

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I would use the OpenG Sort Array function, or similar, like this:

 

post-3889-0-90234000-1420749931.png

Edited by GregSands

Share this post


Link to post
Share on other sites

Just thought of a couple more ideas for you which use less memory and may be quicker (I haven't benchmarked them):

  1. Instead of returning the sorted indices array and searching it, you can just find the location as you create the string array, like this:
    post-3889-0-70308000-1420771444_thumb.pn
     
  2. 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:
    post-3889-0-76502700-1420771460_thumb.pn
    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 by GregSands

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.