LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Weekly Nugget 03/29/2010

When helping one of my colleagues in LabVIEW R&D with some tree control issues last week, he was surprised to learn that there is a built-in sorting method for tree items:

 

 sort_tree.png

 

When using the Edit Tree Items.Sort Children method with the configuration above, all the children of 'parent' (including nested children) will be sorted alphabetically, and case-insensitive. If you want to sort your entire tree in this manner, you would simply wire an empty string to the Tag parameter.

 

But what about more complicated sorting?  There might be cases where you need to sort tree items on something other than, or in addition to, their alphabetical order.  For example, take a look at the VI Analyzer Results Window:

 

results_window.png

 

As you can see, the high-ranked tests are at the top of the list, the low-ranked tests are at the bottom, and all tests are sorted alphabetically within each category.  As you might suspect, the built-in tree sorting method does not allow us to sort based on multiple criteria like this.  There is, however, a VI that ships with LabVIEW that allows us to define our own custom sorting VI for tree elements.  Here it is:

 

tree_sort_vi.png

 

With Tree Sorter.vi, you provide similar inputs (tree reference, Sort Children?, Reverse?, and tag) that you do for the built-in method, but notice you also provide a Sort Spec VI.  This is a VI that you write that defines the sorting rules for the tree.  Let's take a look at the Sort Spec VI for the VI Analyzer Results Window I showed above:

 

sort_spec_vi.png

 

As you can see, the Sort Spec VI takes in two tags, and outputs which of those tags should be above the other in sort order.  So all you need to provide is logic that will determine which of two arbitrary tags should appear above the other one once your tree is sorted.  As you can see, my logic compares the glyphs of the two tags first, and only if they are equal does it use alphabetical ordering to determine the sort order.  The real value of Tree Sorter.vi to me is that I only needed to write the sorting rule itself, and I didn't need to write all the code that went through the entire tree and applied the sorting rule. 

Message 1 of 4
(5,114 Views)
This is very cool, but ... Why isn't this available for MCLs? I seem to recall a certain "captain" stating that MCLs and Trees were the same under the hood.
0 Kudos
Message 2 of 4
(5,076 Views)

Gleichman wrote:
This is very cool, but ... Why isn't this available for MCLs? I seem to recall a certain "captain" stating that MCLs and Trees were the same under the hood.

I think the answer is historical. The tree sorting functionality has been around since the tree control's inception in LabVIEW 7.0.  Trees/Listboxes/MCLs didn't become "the same under the hood" until around LabVIEW 8.0 I believe.  I assume that the tree sorting functionality simply wasn't included in the unifying rearchitecture.

0 Kudos
Message 3 of 4
(5,066 Views)

I had a look at these .llb's (LV 7.1) and they are not pwd protected. So it should be an easy job to migrate/modify them to work on MCLs as well.

 

Seems like there is also some more 'hidden' in there to make some nuggets (lovely queue based stack implementation to manage the recursion, the traverse llb, ...). This will be definitively helpful to me on a personal project, thank you Darren!

 

Felix 

0 Kudos
Message 4 of 4
(4,932 Views)