David wrote on 04 mei 2005 in comp.lang.javas cript:[color=blue]
> I'm having a bit of difficulty sorting images named in sequential order.
>
> image1.jpg[/color]
This will do a alphanumeric sort:
=============== =======
s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
"Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message
news:Xns964CB61 B18CB5eejj99@19 4.109.133.29...[color=blue]
> David wrote on 04 mei 2005 in comp.lang.javas cript:[color=green]
> > I'm having a bit of difficulty sorting images named in sequential order.
> >
> > image1.jpg[/color]
>
> This will do a alphanumeric sort:
>
> =============== =======
>
> s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"
>
> s = s.split(',').so rt().join(',')
>
> alert(s)
>
> =============== ========
>
> resulting in:
>
> image1.jpg,imag e111.jpg,image2 .jpg,image4.jpg ,image9.jpg
>
> --
> Evertjan.
> The Netherlands.
> (Replace all crosses with dots in my emailaddress)
>[/color]
JRS: In article <eq6ee.28100$r8 1.1128@trnddc02 >, dated Wed, 4 May 2005
16:06:34, seen in news:comp.lang. javascript, David <right@dd.com > posted
:[color=blue]
> I need the result like so...
>
>image1.jpg,ima ge2.jpg,image4. jpg,image9.jpg ,image111.jpg[/color]
In such cases, it is far better to arrange that the numeric part is
fixed-width, by using leading zeroes.
If there is much data, I suggest first transform it to that form, then
use a standard sort, then strip leading zeroes.
Otherwise, use a user-defined comparison function :
* Apply a RegExp to each of the comparands, splitting into letter and
number parts.
* Compare the letter parts, as strings, with > and <, returning +1 or -1
accordingly
* If not yet returned, return the difference of the numeric parts; use
of subtraction forces conversion to numbers.
Note that a comparison function is generally called more than N times in
sorting N items, so the entries will be split more than 2N times.
Comment