LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Weekly Nugget 02/16/2009

The Sort 1D Array function is pretty handy.  The help for this function indicates that it can be used to sort a 1D array of clusters.  In this case, the first element of the cluster (based on the cluster order) is used for the sort...for clusters where the first element has the same value, the second element of the cluster is used, etc.  One situation where I've found this functionality useful is sorting an array of strings.  Usually, you want to sort string arrays without worrying about the strings being uppercase/lowercase.  Unfortunately, the ASCII values of the strings are used when sorting, so a simple sort would result in the following:

 

 

 

This is probably not the result you wanted.  So the technique I use is to sort an array of clusters of two elements...the first element is an all lowercase version of the string, and the second element is the original string.  After the array is sorted, I then unbundle the second element from each cluster to generate an array of sorted strings:

 

 

 

-D

P.S. - Check out past nuggets here

Message Edited by Darren on 02-16-2009 11:51 AM
Download All
Message 1 of 7
(7,363 Views)

Darren,

 

Nice idea and a great icon for your sub vi.  Very descriptive.  Smiley Very Happy

0 Kudos
Message 2 of 7
(7,337 Views)

It's also worth noting that OpenG has a useful sort array VI which uses this technique to return the sorted indices (although with an I32), which you can then use if you want to index based on a sorted array. I've used a similar idea to sort an array of numeric strings by logical order (convert the strings to numbers, sort the numeric array and use the sorted indices to index the original array). 

 

P.S. Darren, you might find that your posts will benifit from the Code Capture Tool.


___________________
Try to take over the world!
Message 3 of 7
(7,314 Views)

Hey Darren,

 

Very nice. Perhaps these nuggets would also be appropriate in the NI Community? ni.com/community

Jeff | LabVIEW Software Engineer
Message 4 of 7
(7,224 Views)

Why doe this Nugget make me think I read this before?

 

Attached is a zip of some sand-box code that I developed back in LV 6 to allow me to clone a complete hiarchy so I can wite code once then clone it of to use multiple times in an app. To avoid corss links between the the clone and the cloned I had to save files starting at the bottom of the hiarchy then work my way up. So I had to sort the VIs in momory according to where they appeared the hiarchy. So some helper code determined the depth of each VI so all I had to do was use the "Sort 1-d Array" followed by a reverse 1-d array and I had an array with the bottm VIs first and the top level VI last.

 

Here is where the sort was used

 

 

I bundled up a couple of other items like num of cllers since this code was originally used to prepare to display a "3d Hiarchy" where I wanted the most called sub-VIs to be located cloeser to the center of the structure. 

 

Note: THis is/was sand-box code that was nver intended for public consuption. Please do not take this code as an example of how to code. In LV 8.X you really don't need this VI since the project will do a lot of this work.

 

Thank you,

 

Ben

 

Instructions:

1) Open Clone_VI_Hierarchy

 

2) Use "Source v path" to specify a top level VI that should be cloned

 

3) Use "target floder' TO point where you want the clone to be saved.

 

4) Use "Clone_Prefix" if you want to add a prefix as the VIs are cloned.

 

5) Set "clone" true if you want to do the the cloning.

 

run the VI

Message Edited by Ben on 02-17-2009 10:02 AM
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Download All
Message 5 of 7
(7,208 Views)

Darren,

 

Great post. I'm still telling customers about the Sort 1D. You didn't touch on the implementation details much and I can understand why the implementation should be kept different from the interface.

 

Do you know if the Sort 1D uses an efficient sorting technique like Quick Sort?

 

Best,

 

--

Evan R.

Field Engineer | Western PA | WV

National Instruments

Message 6 of 7
(6,841 Views)
Yes, the Sort 1D Array function uses a version of the Quicksort algorithm.  I assume there are some optimizations for the cluster sorting case, but I don't know any specific details.
0 Kudos
Message 7 of 7
(6,830 Views)