Jump to content
Sign in to follow this  
grmc

Alphanumeric Sorting of an Array? (Brain Bender!)

Recommended Posts

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

Share this post


Link to post
Share on other sites
Here you go:

post-2-1101951537.gif?width=400

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :yes:

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

Share this post


Link to post
Share on other sites
with a slight modification that includes the text and numbers in the sort, I think

this may be a what were are looking for.

post-584-1102086210.png?width=400

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 :blink:

Download File:post-854-1102304293.vi

Share this post


Link to post
Share on other sites
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  :blink:

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.

post-584-1102347056.png?width=400

let me know what you think

Download File:post-584-1102347097.vi

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.