Problem: We have an array of a determinate cluster with different types of data. And we want it to sorted using an specific criteria, but that criteria is not only one field, but several fields in order of priority.
Solution: I guess there should be plenty of solutions to solve this problem, (please leave you comment about how you will do it) but I found this one quite easy to implement and to maintain afterwards. So before anything, lets crack on on the method.
Figure 1 shows the LabVIEW front panel containing the input Array with 4 different fields (2 integers 1 double and 1 string), Also showing the output array once sorted out.
Figure 1: Front Panel |
Figure 2 Shows the LabVIEW Block Diagram, We start with a for loop to get each cluster from the array, we unbundle by name that cluster and bundle it again, but this time, specifying the order criteria we want. in this example we want Field 2 as the top priority then Field 1, Field 4 and last Field 3, note that doesn't matter the data type, it till be sorted out at the end.
Once we bundle we output this new cluster with indexing enabled (this will create a new 1D array). Then we use the sort 1D array function to actually do the sorting process.
Here you have to choices, if the cluster is not a custom control, meaning that you don't care the appearance or other attributes, you can use the output of the sort 1D array as your final output.
But if you indeed had a custom control as your input, you need to convert it to. using the unbundle and bundle within a for loop the last one using as a prototype your input cluster you will have your same input cluster but now completely sorted with your given priority.
Figure 2: Block Diagram |
Please leave in the comments section if you have used this example in you project, or any other questions you might have, I will be more than happy to help you.
Cheers
No comments:
Post a Comment