grmc Posted December 2, 2004 Report Share Posted December 2, 2004 Hi all, Here is a brain bender for (some) of you Basically I have a file list, which is stored in an array. The file names are in alphanumeric format, and it is proving difficult to sort the file names in a logical order. Example: File names are - AB 1.vi AB 2.vi AB 3.vi AB 10.vi AB 11.vi AB 12.vi If these files are in a directory, and a List Directory vi is run to get the filenames, the resulting array will be in this order: AB 1.vi AB 10.vi AB 11.vi AB 12.vi AB 2.vi AB 3.vi Does anyone know a way around this problem (please keep in mind that my file list array is a lot bigger than this example!!!) Regards, grmc Quote Link to comment
Michael Aivaliotis Posted December 2, 2004 Report Share Posted December 2, 2004 Here you go: Quote Link to comment
grmc Posted December 2, 2004 Author Report Share Posted December 2, 2004 Here you go: 2831[/snapback] Hrmm, thats fine for file names with set lengths, but if the directory has more complex names it again proves difficult. e.g. AB 1 AB 10 AB 10.1 AB 10.2 AB 10.10 XYZ 1.3 XYZ 123.4 XYZ 10.2 Really Long File Name 10.3.4 Really Long File Name 10.3.1 Really Long File Name 1.3.4 I was thinking something more along the lines of: Converting each individual character from an array element it to ASCII, add up each array element's ASCII values to work out which order each element should go in, and reinsert into the array back in string format. How feasible is this? I might give it a try :S Quote Link to comment
Michael Aivaliotis Posted December 2, 2004 Report Share Posted December 2, 2004 Hrmm, thats fine for file names with set lengths, but if the directory has more complex names it again proves difficult. Well hey, you never said "universal" I was assuming that the AB[space] was constant and the only thing changing would be the number. AB 1 AB 10 AB 10.1 AB 10.2 AB 10.10 XYZ 1.3 XYZ 123.4 XYZ 10.2 Really Long File Name 10.3.4 Really Long File Name 10.3.1 Really Long File Name 1.3.4 Hmm, what do you mean by 10.1 and 10.10 etc? Do you have dots in the filename? Also, what is the end purpose of all this. Why would you have vi's named this way? Is this some sort of revision control system on your vi's or are you trying to do multiple instantiation? Converting each individual character from an array element it to ASCII, add up each array element's ASCII values to work out which order each element should go in, and reinsert into the array back in string format. How feasible is this? I might give it a try :S 2832[/snapback] You could do that but does that guarantee sorting? Don't know. One way that will work for sure is to use a defined separator between your number and the text name. For example a dash (-) or underscore (_). Ex: Really Long File Name-1.3.4 This way you can use my method and replace the Scan From String function with a match pattern function and just extract the number portion that way. Quote Link to comment
Dan Press Posted December 2, 2004 Report Share Posted December 2, 2004 Check this out. I was experimenting with the error behavior of the Scan from String function. This VI will sort an array of strings by treating any characters that are numeric as actual numbers. That way, you avoid the old 1, 10, 2 ordering. It's something I just whipped up, so there may be a bug or two in there. Attachment is a LabVIEW 7.1 VI. Download File:post-123-1102017466.vi Daniel L. Press PrimeTest Corp. www.primetest.com Quote Link to comment
Michael Aivaliotis Posted December 2, 2004 Report Share Posted December 2, 2004 It's something I just whipped up, so there may be a bug or two in there. 2878[/snapback] I think it does. 12.txt shows up at the top of the list but it should come after 10.2.txt. Also 10.txt should come before 10.1.txt Quote Link to comment
grmc Posted December 2, 2004 Author Report Share Posted December 2, 2004 Hmm, what do you mean by 10.1 and 10.10 etc? Do you have dots in the filename? Also, what is the end purpose of all this. Why would you have vi's named this way? Is this some sort of revision control system on your vi's or are you trying to do multiple instantiation? 2879[/snapback] Let's just say that it's not important what format they are named in, as the files in reality could be named anything One way that will work for sure is to use a defined separator between your number and the text name. For example a dash (-) or underscore (_). Ex: Really Long File Name-1.3.4 2879[/snapback] Yes this would in fact work fine, but it defeats the purpose of the excercise The vi I have created to do the sorting via an algorithm I made up works a little better, but is still not 100% correct. I know there is an algorithm out there somewhere that works, I am just yet to find it! For the sake of the excercise, lets just say that the vi I require forms part of a File Explorer, in which case all sorts of files need to be sorted in normal logical order Quote Link to comment
Dan Press Posted December 2, 2004 Report Share Posted December 2, 2004 I think it does. 12.txt shows up at the top of the list but it should come after 10.2.txt. Also 10.txt should come before 10.1.txt 2879[/snapback] Well, the 12.txt is at the top since it has no text before the number. I could switch the enum to have Text before Number and everything would come out the way you expect, although I thought that people usually like numbers sorted before text in general. If we define the sort order as follows... 1-9, A-Z, a-z ... then 12.txt should come before AB 10.txt. However, I agree that the 10.2.txt comming before 10.txt is odd. Its just that the "2" is before the "t" in the sort order. I could add some code to handle the decimal points differently perhaps, but even then there could be a problem. - Dan Quote Link to comment
grmc Posted December 2, 2004 Author Report Share Posted December 2, 2004 Well, the 12.txt is at the top since it has no text before the number. I could switch the enum to have Text before Number and everything would come out the way you expect, although I thought that people usually like numbers sorted before text in general. If we define the sort order as follows...1-9, A-Z, a-z ... then 12.txt should come before AB 10.txt. However, I agree that the 10.2.txt comming before 10.txt is odd. Its just that the "2" is before the "t" in the sort order. I could add some code to handle the decimal points differently perhaps, but even then there could be a problem. - Dan 2885[/snapback] It's definately a tough one, isn't it!? Good for the ol' brain though :headbang: Quote Link to comment
Michael Aivaliotis Posted December 3, 2004 Report Share Posted December 3, 2004 The vi I have created to do the sorting via an algorithm I made up works a little better, but is still not 100% correct. 2884[/snapback] Care to share? Quote Link to comment
Jim Kring Posted December 3, 2004 Report Share Posted December 3, 2004 Here's my "semi-universal" solution. Download File:post-17-1102054370.vi Quote Link to comment
Mark Balla Posted December 3, 2004 Report Share Posted December 3, 2004 Here's my "semi-universal" solution. Download File:post-17-1102054370.vi 2891[/snapback] with a slight modification that includes the text and numbers in the sort, I think this may be a what were are looking for. Quote Link to comment
grmc Posted December 6, 2004 Author Report Share Posted December 6, 2004 with a slight modification that includes the text and numbers in the sort, I thinkthis may be a what were are looking for. 2899[/snapback] Still seems to be a little out. Heres the one I was working on anyway, if you are interested. Its Nearly correct :S It seems to at least group similar names anyway Download File:post-854-1102304293.vi Quote Link to comment
Mark Balla Posted December 6, 2004 Report Share Posted December 6, 2004 Still seems to be a little out.Heres the one I was working on anyway, if you are interested. Its Nearly correct :S It seems to at least group similar names anyway 2941[/snapback] OK lets try this one. Asumption are no more that 4 revision levels (1.2.3.4) revision levels are seperated by a dot (.) no numbers allowed in file name. here is how my test ran. let me know what you think Download File:post-584-1102347097.vi Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.