LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sort 2D Array - Not sorting string numbers correctly

Solved!
Go to solution

Hi guys,

 

If we have a 2D Array of strings, which happen to be numbers of different magnitudes, when we run it through the sort2Darray.vi, why doesn't it sort those numbers correctly?

 

In the attached VI:

If we sort by column 0, which only has numbers from 1-9- it sorts correctly,

If we sort by column 2, which only has letters- again, it sorts correctly,

If we sort by column 1, which has numbers below and beyond 9, it pools all the 1's together, 2's together and so on...

 

Am I missing something or it this a bug?

 

JohnatanBravo_0-1711723626294.png

 

0 Kudos
Message 1 of 8
(725 Views)
Solution
Accepted by topic author JohnatanBravo

@JohnatanBravo wrote:

Am I missing something or it this a bug?


This is the correct behavior.  Strings are sorted by ASCII values.  Just the same as "ABC" comes before "B", "10" comes before "2".  You should give this idea a kudo to tell NI you would like this feature: Use numeric sorting rather than literal sorting 


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 8
(691 Views)

Ah, OK, I'll have to figure out another way to sort my tables then. Thanks for a prompt response!

 

Do any of the ideas from the Idea Exchange ever get to see the light of day? This one is just over 9 years old!

0 Kudos
Message 3 of 8
(679 Views)

If these columns are just integers, you can detect that and change sorting accordingly. Here's a quick draft.

 

The problem can be significantly more complicated if there are duplicate in a column and you do a secondary sort hierarchically by other column values.

 

Where does the data come from? For example if you would format the integers with sufficient amounts of leading zeroes, numeric sorting=alphabetic sorting.

 

altenbach_0-1711739128586.png

 

Message 4 of 8
(614 Views)

Hi Altenbach,

 

This data comes from an SQL table, and a column which is an identity specifier integer- so it's never duplicated.

 

I was going to spend a chunk of today's morning coming up with the logic, but your solution does exactly what I need!

I hope you don't mind if I borrow it, thanks.

 

0 Kudos
Message 5 of 8
(144 Views)

@JohnatanBravo wrote:

Hi Altenbach,

 

This data comes from an SQL table, and a column which is an identity specifier integer- so it's never duplicated.

 


If it's always integers you can make it even easier by just converting the array to an integer array and use that. (String to decimal number even takes 2D arrays as input for a 1 block conversion)

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 8
(138 Views)

@Yamaeda wrote:
If it's always integers you can make it even easier by just converting the array to an integer array and use that. (String to decimal number even takes 2D arrays as input for a 1 block conversion)

In LabVIEW, a "table" is an indicator for a 2D array of strings and we cannot mix datatypes across columns (yet), Some of the other columns are obviously not integers in this example. My code just temporarily converts the relevant column to integers to create the sort key.

0 Kudos
Message 7 of 8
(119 Views)

@altenbach wrote:

In LabVIEW, a "table" is an indicator for a 2D array of strings and we cannot mix datatypes across columns (yet), Some of the other columns are obviously not integers in this example. My code just temporarily converts the relevant column to integers to create the sort key.


Right, in the preview it's not clear that the 3rd column was letters. 🙂

Still, my post stands. IF it's all integers, it's easier to convert it to that and convert back to strings for display if needed.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 8
(110 Views)