LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to - 2 level sort for a 2D array

I have a 2D array, about 25000 rows and 100 columns. I want to do a 2 level sort based on user selectable two columns. Similar to the sorting done in Excel. So, for example, I could say level 1 sort is per column 4 and level 2 sort is per column 16.

 

How do I go about this?

 

I have LV 8.5 on Windows Vista 64-bit.

Gurdas Sandhu, Ph.D.
ORISE Research Fellow at US EPA
0 Kudos
Message 1 of 6
(4,425 Views)

There are no built in functions in LabVIEW that will sort 2-D arrays.

 

But take a look at Sort Array in the OpenG array toolkit.  It has a function to sort 2-D arrays either by row or column, and you can specify the column number.

 

If your run your array through that twice, once for the column that is the secondary sort, then do it again for the column that is the primary sort, you should get what you want.

 

Otherwise, you'd have to implement your own sorting algorithm.

Message 2 of 6
(4,424 Views)

Thanks, Raven Fan. I tried and your suggestion worked in principle. I'll have to make some minor changes because my input array is alphanumeric and one of my sort column could be numbers while the other is alphabets.

 

That said, I had a question about the OpenG vi implementation. When they are construction the sorted array (section labeled "Sort 2D Array by Pointer"), they let the For Loop indexing function (at output edge) build the array. Does this mean frequent memory relocation? Would it be more efficient to initialize an array (each element is "NaN") outside the loop and use the 'Replace Array Subset' function inside the loop? My array could be up to 50000 x 200 and I want to minimze time to complete task.

Or, is it that since they've indexed the input edge of the For Loop, LV knows what memory to block so this method is not inefficient?

Gurdas Sandhu, Ph.D.
ORISE Research Fellow at US EPA
0 Kudos
Message 3 of 6
(4,397 Views)

Another update, referring to the post http://forums.ni.com/t5/LabVIEW/Sort-2D-Array-With-Header/td-p/1405750

 

I believe the solution offered is when the same column can have both numbers and alphabets (not my case). What is interesting is the idea that LV's 'Sort 1-D Array' looks at second element in a cluster when there is a tie for the first element. So, does that mean if I follow nathand's solution with below given change to step 2, I am all set?

 

(2) From my original array, pull out my two sort columns and in a For Loop build a three element cluster where:

Element 1 = Level 1 sort column element

Element 2 = Level 2 sort column element

Element 3 = iteration count

 

Gurdas Sandhu, Ph.D.
ORISE Research Fellow at US EPA
0 Kudos
Message 4 of 6
(4,395 Views)

@Gurdas wrote:

 I am all set?


(In the time you needed to write up that post, you could have written some simple LabVIEW code to test it out. :D)

 

But yes, thay will work.

0 Kudos
Message 5 of 6
(4,392 Views)

Altenbach .... arrggghh ... true, but then I wouldn't have had the pleasure of reading your comment  🙂

 

Gurdas Sandhu, Ph.D.
ORISE Research Fellow at US EPA
Message 6 of 6
(4,378 Views)