03-29-2024 09:47 AM - edited 03-29-2024 09:58 AM
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?
Solved! Go to Solution.
03-29-2024 10:11 AM
@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
03-29-2024 10:37 AM
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!
03-29-2024 02:05 PM - edited 03-29-2024 02:08 PM
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.
04-02-2024 02:16 AM
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.
04-02-2024 02:29 AM
@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)
04-02-2024 08:57 AM
@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.
04-02-2024 09:49 AM
@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.