473,560 Members | 2,959 Online

# sort()

sorry for the last post, itchy fingers.

I'm having a bit of difficulty sorting images named in sequential numerical
order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I've tried a few methods unsuccssfully.

David

Jul 23 '05 #1
15 3192
David wrote on 04 mei 2005 in comp.lang.javas cript:
sorry for the last post, itchy fingers.

I'm having a bit of difficulty sorting images named in sequential
numerical order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I've tried a few methods unsuccssfully.

Why, it is a corect result of an alphanumeric sort?

[see my earlier posting]

However, if you want a numerical sort:

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

s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"

s = s.split(',').so rt(compare).joi n(',')

document.write( s)

function compare(a, b) {
a = +a.replace(/image/g,'').replace(/\.jpg/g,'')
b = +b.replace(/image/g,'').replace(/\.jpg/g,'')
if (a<b) return -1
if (a>b) return 1
return 0
}

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

this returns:

image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
On Wed, 04 May 2005 15:52:32 GMT, "David" <ri***@dd.com > wrote:
sorry for the last post, itchy fingers.

I'm having a bit of difficulty sorting images named in sequential numerical
order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I've tried a few methods unsuccssfully.

David

For the names you have, this is the correct alphanumeric sort - the
sort routine doesn't know to treat the digits at the end of the name
as numbers. Rename your images so they always have the same number of
digits in the name, and that will fix it (i.e. image01.jpg,
image02.jpg...i mage09.jpg,imag e10.jpg).

Of course, this will break if you use more than 99 images and you'd
have to add another digit!

Paul
Jul 23 '05 #3
Paul Cooper wrote on 04 mei 2005 in comp.lang.javas cript:
For the names you have, this is the correct alphanumeric sort - the
sort routine doesn't know to treat the digits at the end of the name
as numbers.

True, Paul, but it can learn, if you teach it.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
David wrote:
sorry for the last post, itchy fingers.

I'm having a bit of difficulty sorting images named in sequential numerical order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I've tried a few methods unsuccssfully.
David

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<script type="text/javascript">

Array.prototype .sortPix = function()
{
return this.sort(
function(a, b)
{
return (a.match(/\d+/) - b.match(/\d+/));
}
);
}

var pix = [
'image7.jpg' ,
'image9.jpg' ,
'image4.jpg' ,
'image11.jpg' ,
'image10.jpg' ,
'image01.jpg' ,
'image6.jpg' ,
'image12.jpg' ,
'image03.jpg' ,
'image8.jpg' ,
'image2.jpg' ,
'image5.jpg'
];

var sorted = pix.sortPix();

</script>
<body>
</body>
</html>

Jul 23 '05 #5
Evertjan. wrote:
David wrote on 04 mei 2005 in comp.lang.javas cript:

sorry for the last post, itchy fingers.

I'm having a bit of difficulty sorting images named in sequential
numerical order. Here are the image names and how I need them sorted.

image1.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg
image10.jpg
image11.jpg
image12.jpg

Using array.sort() here is how it gets sorted,

image1.jpg
image10.jpg
image11.jpg
image12.jpg
image2.jpg
image3.jpg
image4.jpg
image5.jpg
image6.jpg
image7.jpg
image8.jpg
image9.jpg

Any ideas how to correct this? I've tried a few methods unsuccssfully.

Why, it is a corect result of an alphanumeric sort?

[see my earlier posting]

However, if you want a numerical sort:

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

s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"

s = s.split(',').so rt(compare).joi n(',')

document.write( s)

function compare(a, b) {
a = +a.replace(/image/g,'').replace(/\.jpg/g,'')
b = +b.replace(/image/g,'').replace(/\.jpg/g,'')
if (a<b) return -1
if (a>b) return 1
return 0
}

or:

function compare(a,b){
return a.replace(/\D/g,'') - b.replace(/\D/g,'');
}
s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"

Mick

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

this returns:

image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg

Jul 23 '05 #6
For the names you have, this is the correct alphanumeric sort - the
sort routine doesn't know to treat the digits at the end of the name
as numbers. Rename your images so they always have the same number of
digits in the name, and that will fix it (i.e. image01.jpg,
image02.jpg...i mage09.jpg,imag e10.jpg).

Of course, this will break if you use more than 99 images and you'd
have to add another digit!

Paul

If it were that simple as to rename the images I would, but this will be an
application where users can upload thier own images with thier own naming
conventions so I need to make corrections for this neforehand.

David
Jul 23 '05 #7

You guys are great and have given me things to try. Your examples do work
but not exactly the way it needs to be. I have no control over the image
names. This will be determined by "users", but they need to be sorted in a
specific manner because "users" are dumb and use all kinds of naming
conventions so I'm trying to correct for this beforehand in the script.

Your codes do work but they sort soley by numerical. So imagine the images
are named like this..

lightimage1.jpg
darkimage1.jpg
darkimage10.jpg
blueimage2.jpg

Sorting them alphanumericaly only will result in this..

darkimage10.jpg ,darkimage1.jpg ,blueimage2.jpg ,lightimage1.jp g
Sorting them numericaly only will result in this..

blueimage2.jpg, darkimage1.jpg, lightimage1.jpg ,darkimage10.jp g
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g

It sorts the image names alphanumericall y but .. those that have numerical
endings will be sorted in the correct order. Hard to explain but I think
you know what I mean. Is this possible?

David

function compare(a,b){
return a.replace(/\D/g,'') - b.replace(/\D/g,'');
}
s = "image4.jpg,ima ge9.jpg,image11 1.jpg,image2.jp g,image1.jpg"

Mick

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

this returns:

image1.jpg,imag e2.jpg,image4.j pg,image9.jpg,i mage111.jpg

Jul 23 '05 #8
Correction to last post. I had the first example wrong in the
alphanumericall y only sorted. The blueimage2.jpg would come first in the
array.

Your codes do work but they sort soley by numerical. So imagine the images
are named like this..

lightimage1.jpg
darkimage1.jpg
darkimage10.jpg
blueimage2.jpg

Sorting them alphanumericaly only will result in this..

blueimage2.jpg, darkimage10.jpg ,darkimage1.jpg ,lightimage1.jp g
Sorting them numericaly only will result in this..

blueimage2.jpg, darkimage1.jpg, lightimage1.jpg ,darkimage10.jp g
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg, darkimage1.jpg, darkimage10.jpg ,lightimage1.jp g
Jul 23 '05 #9
David said:

Correction to last post. I had the first example wrong in the
alphanumerical ly only sorted. The blueimage2.jpg would come first in the
array.

Your codes do work but they sort soley by numerical. So imagine the images
are named like this..

lightimage1.jp g
darkimage1.j pg
darkimage10.jp g
blueimage2.j pg

Sorting them alphanumericaly only will result in this..

blueimage2.jpg ,darkimage10.jp g,darkimage1.jp g,lightimage1.j pg
Sorting them numericaly only will result in this..

blueimage2.jpg ,darkimage1.jpg ,lightimage1.jp g,darkimage10.j pg
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg ,darkimage1.jpg ,darkimage10.jp g,lightimage1.j pg

And what about "darkimage10a.j pg" and "darkimage10b.j pg"?
And what about the user who likes Roman Numerals?

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.