inadmissible

JavaScript natural order string comparison

August 1st, 2006

Both Jonny Reichwald and Greg Nacu mailed me about a little bug in my JavaScript natural order string comparison implementation, so I patched it. Here’s the latest version: natcompare.js

Visit Martin Pool’s natsort project page for the original and other implementations.

Entry Filed under: JavaScript

4 Comments Add your own

  • 1. Ben Reser  |  September 19th, 2006 at 3:00 am

    You might want to get Martin Pool to update the script on the sourceforge site. I bumped into the but and debugged and fixed it myself. I was then looking for where I found it and bumped into your blog and found your already fixed version. But the one on the sourceforge page is still broken.

  • 2. Kristof  |  September 19th, 2006 at 7:08 am

    Hello Ben

    I mailed Martin twice but he does not seem to be very responsive :-)

    Tried again with another address today, I hope the mail reaches his destination now.

  • 3. Braden  |  December 12th, 2006 at 8:24 pm

    Thank you. This script is great. I am adding a case-insensitive option for my own use. If you want to add it into the trunk, I just added this block, which may not be the most efficient way:

    var ia = 0, ib = 0;
    var nza = 0, nzb = 0;
    var ca, cb;
    var result;

    + if (i) {
    + a = a.toLowerCase();
    + b = b.toLowerCase();
    + }
    +
    while (true)
    {

  • 4. sasha  |  March 1st, 2007 at 6:51 pm

    You could add something like this:
    var cm = ca.localeCompare(cb) ;
    if (cm) return cm ;
    Rather then this

    if (ca cb) {
    return +1;
    }
    To compare strings based on system locale

    Additionally if you want case-insensitive comparisson you should not do it in the function itself, but rather send it lower-case arguments like this

    natcompare (arg1.toLowerCase() , arg2.toLowerCase())

    or with locale support:

    natcompare (arg1.toLocaleLowerCase() , arg2.toLocaleLowerCase())

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

August 2006
M T W T F S S
« Jul   Oct »
 123456
78910111213
14151617181920
21222324252627
28293031  

Most Recent Posts