LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to sort a 2D array?

I have a 2D array with 3 columns and 1000 rows and I need to sort all these 1000 rows in terms of their values in the third column.   Now I split the three columns into three individual 1D array and sorted the 1D array of the third column. My question is how to make the first two columns change their orders following the third column?  For exsample:

 

A=[

0  0  -1

1  0  0

2  1  3

5  4  2

6  1  1]

 

how to sort it into:

B=[

2  1  3

5  4  2

6  1  1

1  0  0

0  0  -1];

 

0 Kudos
Message 1 of 6
(10,934 Views)

Move your question to the LabVIEW section of the forum. This section (BreakPoint) is for side discussions, more often than not, unrelated to LabVIEW. There's a trick with using a cluster to hold the original row index and the current value, and then you can sort by the current value and the orginal index goes along for the ride. Then you can use the new order to reorder your array. Clear? haha.

 

Move your question to the LabVIEW forum and people will know what I'm getting at.

0 Kudos
Message 2 of 6
(10,930 Views)

There might be a way to do this without making a copy of the data, but here ya go. I caved 🙂

 

 

 

Edit: I noticed afterwards you needed it to be greatest to least. After you sort the array, do a reverse array and it will do what you want.

0 Kudos
Message 3 of 6
(10,927 Views)

Here is an old related discussion...

0 Kudos
Message 4 of 6
(10,915 Views)

For:

 

Thank u very much for providing me this information!

 

Frank

0 Kudos
Message 5 of 6
(10,894 Views)

Altenbach:

 

Thanks!

 

Frank

0 Kudos
Message 6 of 6
(10,893 Views)