To download NI software, including the products shown below, visit ni.com/downloads.
1. Overview:
The AlphaLogical Bubble Sort will sort string arrays alphabetically and treats numbers within the strings the way people find logical ("a9" prior to "a10" e.g.)
2. Description:
This is now also the standard way of sorting file names in Windows Explorer (introduced in XP). See the below example for the difference between this and the 'traditional' method of sorting.
Traditional LabVIEW Array Sorting (Strings):
Unsorted | Sorted |
---|---|
bravo10.txt | alpha.txt |
charlie.txt | bravo1.txt |
bravo1.txt | bravo10.txt |
alpha.txt | bravo2.txt |
bravo2.txt | charlie.txt |
AlphaLogical LabVIEW Array Sorting (Strings):
Unsorted | Sorted |
---|---|
bravo10.txt | alpha.txt |
charlie.txt | bravo1.txt |
bravo1.txt | bravo2.txt |
alpha.txt | bravo10.txt |
bravo2.txt | charlie.txt |
3. Requirements
4. Steps to Implement or Execute Code
5. Additional Information or References
VI Block Diagram
**This document has been updated to meet the current required format for the NI Code Exchange.**
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Jared,
Thanks for re-posting this code.
People who download the code you posted might want to take a look at an example that I posted which performs a multi-column sort on a 2D array of strings that contains both plain text strings and strings representing real numbers. This example also enables the user to specify the column sort order.
Download the Sort 2D String Array VI
Take a look and let me know your thoughts.
Thanks again.
Mark Ridgley
Hey Mark,
I think that the VI's I've posted are a great segue into the 2D Array sorting VI that you've posted. Users will be able to follow the link you provided to that. Keep up the great work, and thank you for contributing to the community!
- Jared Boothe
Funny, I had forgotten that I made a bubble sort version. There is a more efficient implementation (Quick Sort) by yours truly here as well.
Yet another one that doesn't really work well. Here is an example of what it did:
Unsorted | Sorted |
---|---|
12.12 | 11 |
12 | 101 |
11 | 12.12 |
101 | 12 |
12.11 | 12.11 |
34 | 34 |
System decimal point is dot, but I see the code doesn't care actually. Doesn't sort properly negative numbers as well.
You are right that it does not relate to pure numbers as one might expect, it inteprets it as a string (it was never really designed for pure numbers as a regular sort is an alternative there, but anyway).
I remember using the file name sorting of Windows Explorer as a reference for what is to be "expected", so I did a new test now on Windows 10. Windows will indeed put 101 last, but it will still list 12.2 (and 12,2) prior to 12 I would say is a bit "unexpected" too.
Perhaps I'll have another look at the old code some day, probably to match Windows again. But Penoff - you could have a go at it and make one that satisfies your expectations as well. You are free to use the existing code as a starting point if you want.Please share the result if you do.
I am not sorting pure numbers. That was just an example. Windows is still not providing the perfect sorting, I know, but it is as close as I've seen. That's why I am trying to make use of its sorting function, but it doesn't work well for some reason. What I am trying is shlwapi.dll's function StrCmpLogicalW. So far I get inconsistent results that don't match the behaviour in Windows Explorer nor the function description (I never get 0 as result for example).
Good Morning, Penoff
You may want to have a look at Sort 2D String Array.vi (https://decibel.ni.com/content/docs/DOC-2332)
It appears to output the results you expect – with the caveat that the input array elements are strings. The screenshot below shows the results of a test I ran this morning and the code used to generate the sorted array.
Hope this helps.
It is afternoon for me here in Norway
This one seems to be sorting numbers better, but when the strings are more complex it fails in some cases.
A couple of examples (used in LabVIEW 2015 32Bit)
Unsorted | Sorted |
---|---|
123.456.789 | 123 |
123.453.789 | 123.4530000 |
123 | 123.4560000 |
Unsorted | Sorted |
---|---|
a123.456.789 | 0 |
a123.453.789 | 0.0000000 |
a123 | 0.0000000 |
I finally gave up on searching the Internet and started working on my own tool that will split each string into a string array by extracting all numeric chunks and taking into consideration + and - signs. Hopefully I will be able to make it work not be extremely heavy on CPU and Memory. I'm sure it won't be the most efficient tool, but for now I don't plan to use it for arrays containing millions of strings.
The challenge when developing any sorting algorithm is that one must know something about the data you wish to sort and that data must adhere to certain rules. For example - in the first data set that you posted, you used numbers that can be interpreted as floating point values having a single decimal point (e.g. 12.11). In the second data set you posted, you have alpha-numeric strings having two decimal points (e.g. 123.456.789) so the question is "What do these alphanumeric strings represent? - Telephone numbers, account numbers...?" Once we can answer this question, we can put in place some rules for how to sort the data. Developing a sorting algorithm that will work for every imaginable input data format is nearly impossible as users will always find a case for which a given sorting algorithm does not work. As Mads stated in an earlier reply, you could have a go at it using the existing example code as a starting point and develop a sorting algorithm that satisfies your expectations. Please post the results of your efforts if you do.
You are right, I wasn't clear what I am trying to achieve. I don't have a specific kind of data that I am trying to sort. Usually it is easier to parse some strings of known pattern and then sort them. Most of the time all I need is "logical" sorting of strings that will look properly sorted to the user (alpha-numeric names of items, pure numbers and so on). That is why I was trying to mimic the behaviour of Windows Explorer's sorting. It is not perfect, but is close enough. Since there isn't one perfect algorithm that works for every problem, I am sure mine won't be exception.
I am now close to making it sort two strings "logically" and from there I will make it sort a whole 1D array. I can post it when I make it work (hopefully). I see no option for attaching a file here though.
The quicksort based version I posted back in 2005 has been moved to this location now:
https://forums.ni.com/t5/Example-Code/Alphabetical-and-Numerically-Logical-String-Array-Sort/ta-p/39...