06-11-2018 12:02 PM
Looking for the forum thread on sorting strings/file paths of the numeric format x.x.x.x
(where the number of x's was probably known) and there was a requirement to sort them.
I thought it would be faster to find the thread with the good code than try to re-engineer some inferior code myself.
(trying to find the latest version of a file by file name, version number and extension in a folder)
Thanks in advance to whoever remembers the location or can think of better search terms than I've been trying for the last 45mins.
James
Solved! Go to Solution.
06-11-2018 12:22 PM
I assumes the Xs are integers and you want numeric, not alphabetic sorting. I also assume that within one run, the number of x's is constant.
Assuming numeric sorting, I would autoindex on the names on a FOR loop, for each use "spreadsheet string to array" with "." as delimiter and a 1D U32 array as type, Use array to cluster (Do you have an upper limit on the number of x's? Make the cluster size reasonable large), creating an array of such cluster at the autoindexing output tunnel. Sort that array and reverse the operation. To preserve the exact original formatting, add the original index as last element and, after sorting, use it to index into the original array.
06-11-2018 12:52 PM - edited 06-11-2018 12:57 PM
@altenbach wrote:
I assumes the Xs are integers and you want numeric, not alphabetic sorting. I also assume that within one run, the number of x's is constant.
Assuming numeric sorting, I would autoindex on the names on a FOR loop, for each use "spreadsheet string to array" with "." as delimiter and a 1D U32 array as type, Use array to cluster (Do you have an upper limit on the number of x's? Make the cluster size reasonable large), creating an array of such cluster at the autoindexing output tunnel. Sort that array and reverse the operation. To preserve the exact original formatting, add the original index as last element and, after sorting, use it to index into the original array.
Hi Christian,
I would assume that they are all int too, but as I've picked the code up from a developer who's dropped it and haven't fully interfaced with the client yet, I think I might have to prepared for the possibility of an alphanumeric numbering scheme given the particular customer.
I know there was a thread that dealt with this, especially the difference between e.g. 1.0.1 and 1.0.1.1 (if we were going the numeric route) and had a final VI in it that handled the problem very nicely I just can't find the dratted thing.
I'll have a look into your solution now. For once it doesn't involve complex maths!
******Edit: Sorry, forgot my manners and to say thank you. - So thank you.
Also just noticed the time, so will run now for Bible study group and hit this again first thing in the morning. ('x' max is 4 I think).****************
James
06-11-2018 01:01 PM
For alpha sorting, use a 1D array of strings as type.
For different sort orders, reverse the array or not, and pad the array left or right to the cluster size. The possibilities are endless.
06-11-2018 01:50 PM - edited 06-11-2018 01:51 PM
For natural sort of a 1D string array (e.g. T2 comes before T10), look at the MGI library.
06-11-2018 03:33 PM
If the string is really all numeric positive integers separated by periods, then you can easily create an array of the numbers using Scan from String in a While Loop. For example, the following code produces an integer array of 12, 34, 56, and 78.
Bob Schor
06-12-2018 02:58 AM
So here is my VI explaining the theory of what I am hoping to achieve before I start - As it looks like that thread is not going to resurface but I'm getting lots of great help from some of the real forum greats!
06-12-2018 03:25 AM - edited 06-12-2018 03:27 AM
As usual, Christian has come up with the goods again!
Implementing his code definition results in this VI which does exactly what I want (and highlights a flaw in my posted debug VI code )
I'm still sure this can be optimised futher. The inclusion of the trim whitspace seems a massive overhead each run of the FOR loop - but then given the number of times this will run I shouldn't be worrying too much.
Thanks to all
James
*Edit seem to be having an issue posting the vi snippet*
06-12-2018 10:49 AM
Add [i] to the cluster in the first loop as an additional and last cluster element, then use it to index into the original array in the second loop. All you need in the second loop is "index array".
06-12-2018 12:24 PM
@altenbach wrote:
Add [i] to the cluster in the first loop as an additional and last cluster element, then use it to index into the original array in the second loop. All you need in the second loop is "index array".
Have I missed something? It's already sorted by the sort array function acting on the cluster. I don't want to unsort it and put it back to an original state. I thought the code was working beautifully.
What bug have I missed that's going to come back and bite me please Christian? I can't for the life of me see how [i] helps here.
James