LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

sorting 2D array by col2

hello

i'm using the attached VI for sorting my 2D arrays. Works pretty fine if my column data
is of string type. But in my current Labview projekt I need to sort an array that
has different data types. The sorting should be by an integer column (col2).

a simplified example:

col1 col2

string 5
string 10
string 4
string 24


The attached vi works only for numbers less than ten.

Can anyone help me to modify it or give an advice for an alternative vi ?



Simon
0 Kudos
Message 1 of 6
(7,324 Views)
If all of the strings will be integers, why not just convert the strings to numbers before doing the sort and then convert back to string after. The functions Decimal String to Number and Number to Decimal String will take an array (1D or 2D) as an input. I've attached your VI modified with the conversion functions in there.
0 Kudos
Message 2 of 6
(7,324 Views)

Strings are sorted alphabetically, even if they are numeric, thus 51 goes before 6, etc. same as "Hippopotamus" is smaller than "Mouse", even though the animal AND the "word" is bigger. 🙂

The "number strings" need to be converted to numeric representation (DBL, I32, etc.) as appropriate to get the correct order.

Your icon is very nice, but your code jumps through quite a few uneccessary hoops to achieve this relatively simple task. I have attached a streamlined version that does exactly the same with much simpler code, and sorts numerics if so desired as an option. 🙂 I use "Fract/Exp string to number" for more flexibility, if you only have integers, you could use "Decimal String to number" instead. Modify as needed.

I would also recommend to look into the OpenG toolkit. You will find an 2D sort VI that already is quite flexible (except for the numeric option). My code above is partially based on the openG algorithm.

Many improvements are possible. You might want to add error handling to prevent numeric sorting of non-numeric columns (e.g. column 0 in our case) or even add some extra intelligence. e.g. it could check if the desired column only contains numbers and no other characters, then switch to numeric mode automatically, while using alpha mode in all other cases.

Notice that Dennis's example above does not handle sorting by column 0, because it is not numeric. Since you have mixed fields, this will thus not be sufficient.

Message 3 of 6
(7,324 Views)
Thank you very much for the neat and tidy 2D sort.
Yours Sincerely
John
0 Kudos
Message 4 of 6
(7,324 Views)

I have seen your vi and it is very useful to sort the row of the tables, based in just one column.

 

The problem I have, it is to sort the rows of a table in ascending order base in the column 0 and 1. Both columns could be converted to I32.

 

Example:

 

ID     Test 

120     1

120     2

120     3

121     1

122     2

--

--

 

Thanks for your help.

Simbani

 

 

0 Kudos
Message 5 of 6
(6,091 Views)

Thanks for the clarification on strings-this was exactly what I needed and very much appreciated. I can finally stop banging my head against the wall.

Cheers!

Message 6 of 6
(3,180 Views)