sort()

D

David

I'm having a bit of difficulty sorting images named in sequential order.

image1.jpg
 
E

Evertjan.

David wrote on 04 mei 2005 in comp.lang.javascript:
I'm having a bit of difficulty sorting images named in sequential order.

image1.jpg

This will do a alphanumeric sort:

======================

s = "image4.jpg,image9.jpg,image111.jpg,image2.jpg,image1.jpg"

s = s.split(',').sort().join(',')

alert(s)

=======================

resulting in:

image1.jpg,image111.jpg,image2.jpg,image4.jpg,image9.jpg
 
L

Lee

David said:
I'm having a bit of difficulty sorting images named in sequential order.

image1.jpg


If you're asking for help, you'll have to give us more to work with.
 
D

David

Thanks Evertjan but I need the result like so...

image1.jpg,image2.jpg,image4.jpg,image9.jpg ,image111.jpg

David
 
D

Dr John Stockton

JRS: In article <eq6ee.28100$r81.1128@trnddc02>, dated Wed, 4 May 2005
I need the result like so...

image1.jpg,image2.jpg,image4.jpg,image9.jpg ,image111.jpg


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.

I don't have time to write and test code tonight.
 
D

David

Thanks Dr. John,

I think I have enough to go on thanks to the other responses. If I muck it
up .. I'll be back :)

David





Dr John Stockton said:
JRS: In article <eq6ee.28100$r81.1128@trnddc02>, dated Wed, 4 May 2005



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.

I don't have time to write and test code tonight.
links.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top