LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting 2 Arrays

Solved!
Go to solution

I have two 1D array of same size, the elements are not in ascending/descending order:

Array#1 [3.5    5.1    4.2    6.2    5.4]

Array#2 [2.1    3.1    4.0    3.5    4.3]

 

I want to be able to sort the two arrays (in either ascending or descending order) base on one of the array, i.e if sort ascending based on Array#1, the results would be:

 

Array#1 [3.5    4.2    5.1    5.4    6.2]  <-- this array is sorted (ascending order)

Array#2 [2.1    4.0    3.1    4.3    3.5]  <-- this is not sorted, but the order is such that its elements still correspond to the same element in Array#1

..just like in Excel, you highlight two column, and click the "sort Ascending" button.

How to do this in LV?

 

0 Kudos
Message 1 of 5
(7,597 Views)
Solution
Accepted by topic author splee

Combine your two arrays into an array of clusters where the first element of the cluster is from the first array, and the 2nd element from the second array.  There is a function in the cluster palette called Index and Bundle Cluster Array that will do that for you.

 

Then sort the 1-D array of clusters.  It will sort based on the first element.

 

 

 

To add more data, do the same thing and append the 2nd array of clusters to the first and repeat the sort.

Message 2 of 5
(7,590 Views)

so simple Smiley Very Happy

0 Kudos
Message 3 of 5
(7,577 Views)

Anyone has any idea how to convert the sorted array of clusters back to two separate arrays?


Cluster-to-Array.png@Ravens Fan wrote:

Combine your two arrays into an array of clusters where the first element of the cluster is from the first array, and the 2nd element from the second array.  There is a function in the cluster palette called Index and Bundle Cluster Array that will do that for you.

 

Then sort the 1-D array of clusters.  It will sort based on the first element.

 

 

 

To add more data, do the same thing and append the 2nd array of clusters to the first and repeat the sort.


 

0 Kudos
Message 4 of 5
(7,515 Views)

Iterate through the array to unbundle the elements back into individual arrays.

 

 

 

Then go and kudo this idea.  Why is there no function to reverse "Index & Bundle Cluster Array"

Message 5 of 5
(7,511 Views)