Jump to content

Natural Order String Comparison


Recommended Posts

When sorting arrays, the sorting algorithm needs something to go by to decide what goes before and after what.

most comparisons used work alphabetically (A < B < C < D < E ... < Z) and alphanumerically (A1 < A10 < A2 < A3 < B1)

The problem is that when dealing with versions and other file name specifics, 10 should go after 9, and not between 1 and 2. Natural order comparison solves this problem by doing alphabetical ordering for text, and numerical ordering (0<1<2...<8<9) for number parts of the given string.

This code has been adapted from the JS source I found on the internet. Full copyright information in the attached file.

This can be combined with a custom quicksort to replace _arraySort when dealing with file names with versions in them.

Next step: a launcher for Azureus as a service that will update the jar file to the latest available beta, running it with the latest installed JRE... both purposes deal with names with versions in them, the best use of Natural Order sort.

EDIT: fixed bugs in attached file, uploaded new version

EDIT 2: preliminary natsort function uploaded in quicksortmod.au3... Thx to Jos, Tylo, and LazyCoder for writing the base functions... Even if you don't care about natsort, you might care about this: I modified the functions to take in a programmer or user-specified comparison function, with authentication to prevent security leaks.


Edited by DaProber
Link to comment
Share on other sites

Update: I ripped the required functions from <Array.au3> to get _ArraySort() working without the rest of the include file, so I can modify the structure of the functions to be able to call a comparison function that is user-specifiable, which I will implement using Call() + input authentication (never trust the user). I'd like to start my work by thanking Jos, LazyCoder, and Tylo, for making a great, adaptable sort algorithm which I can now hack apart and hopefully improve... :)

The main drive for this is that if these changes were allowed into the distributed UDF file, it would allow greater flexibility to the scripter, since if they wanted items compared differently, all they would have to do is write their own comparison function and pass it as a parameter to _ArraySortMod()...

Since I haven't yet done any modifications to the _ArraySort() functions, and I am still planning on how to do this, anybody have any ideas, possible pitfalls, suggestions, warnings, or anything constructive at all to say, please reply to this thread... Of course, implemented ideas will be attributed to its contributor...

For those interested, I'm thinking of following the c sort API, in which for items A and B, the function will return 1 if A > B, -1 if A < B, or 0 if A = B

String/Array Comparison) does: negative if the first argument is "less" than the second, zero if they are "equal", and positive if the first argument is "greater".

Edited by DaProber
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...