LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2D array sorting with conditions

Solved!
Go to solution

I have a 2D array in the form as followed

1,1

1,3

2,1

2,2

2,3

3,1

4,1

4,2

5,1

5,5

I want to sort it (based on the second column) only when the element in the first column is even, for example, new array should be:

1,1

1,3

2,3

2,2

2,1

3,1

4,2

4,1

5,1

5,5

 

I have tried many days but failed. Thank you so much!

 

 

Download All
0 Kudos
Message 1 of 5
(3,299 Views)

There are probably several different ways to attack this.

 

Here is the way I would try first. 

It is always a 2-D array that has just 2 columns?

Take the array and iterate through it row by row.  Build a cluster that contains the column 0 value, the column 1, value.  If yo do this with an auto-indexing For Loop, you'll wind up with a 1-D array of clusters.

 

Sort that 1-D array of clusters.  (one question though, it looks like your "sorted" array has the even numbers in reverse order.  That might complicate the sorting process, but there could be other ways around that.

 

With the Sorted 1-D array, go through element by element.  If the first item in the cluster is odd, just build the elements back into a 1-D array.  If that element is even, then take the row from the original, unsorted array.  Do that in a For Loop and you'll have a 2-D array of original odd numbers, and sorted event numbers.

Message 2 of 5
(3,247 Views)
Solution
Accepted by topic author Jinghang

Something like that I guess...

That property nodes were Array in local vars at different steps to compare side by side... Snippet breaks them =( VI is correct

Custom sort.png

Message 3 of 5
(3,240 Views)

Alexander_Sobolev wrote:... Snippet breaks them =( VI is correct

Use the Code Capture Tool you can install from VI Package Manager.  It doesn't break snippets the way the built-in VI snippet maker does.

Message 4 of 5
(3,232 Views)

Thank you all for the answers.

 

problem solved with Alexander_Sobolev's program.

0 Kudos
Message 5 of 5
(3,213 Views)