By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

sort()

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
David wrote on 04 mei 2005 in comp.lang.javascript:
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,image9.jpg,image111.jpg,image2.jpg,ima ge1.jpg"

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

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,image2.jpg,image4.jpg,image9.jpg,image1 11.jpg

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

Jul 23 '05 #2

P: n/a
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...image09.jpg,image10.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

P: n/a
Paul Cooper wrote on 04 mei 2005 in comp.lang.javascript:
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

P: n/a
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>
<head>
<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();
alert(sorted.join('\n'));

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

Jul 23 '05 #5

P: n/a
Evertjan. wrote:
David wrote on 04 mei 2005 in comp.lang.javascript:

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,image9.jpg,image111.jpg,image2.jpg,ima ge1.jpg"

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

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,image9.jpg,image111.jpg,image2.jpg,ima ge1.jpg"
alert(s.split(",").sort(compare).join("\n"))

Mick

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

this returns:

image1.jpg,image2.jpg,image4.jpg,image9.jpg,image1 11.jpg

Jul 23 '05 #6

P: n/a
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...image09.jpg,image10.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

P: n/a

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,ligh timage1.jpg
Sorting them numericaly only will result in this..

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

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,ligh timage1.jpg

It sorts the image names alphanumerically 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,image9.jpg,image111.jpg,image2.jpg,ima ge1.jpg"
alert(s.split(",").sort(compare).join("\n"))

Mick

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

this returns:

image1.jpg,image2.jpg,image4.jpg,image9.jpg,image1 11.jpg

Jul 23 '05 #8

P: n/a
Correction to last post. I had the first example wrong in the
alphanumerically 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,ligh timage1.jpg
Sorting them numericaly only will result in this..

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

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,ligh timage1.jpg
Jul 23 '05 #9

P: n/a
Lee
David said:

Correction to last post. I had the first example wrong in the
alphanumerically 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,lig htimage1.jpg
Sorting them numericaly only will result in this..

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

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,lig htimage1.jpg


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

Jul 23 '05 #10

P: n/a
> And what about "darkimage10a.jpg" and "darkimage10b.jpg"?
And what about the user who likes Roman Numerals?

Good point, but the sort for an image ending in an alphanumeric character
would then be sorted alphanumerically so it isn't an issue, and it will sort
them properly "automatically".
"darkimage1a.jpg"
"darkimage10a.jpg"
"darkimage10b.jpg"

but, even with these the problem still exists of the 1, 10 or 2, 20 etc..
syndrome.

"darkimage1a.jpg"
"darkimage10a.jpg"
"darkimage10b.jpg"
"darkimage1b.jpg"

when it should be

"darkimage1a.jpg"
"darkimage10a.jpg"
"darkimage1b.jpg"
"darkimage10b.jpg"

It's just the numbers that appear last in the image name with reference to
1, 10, 2, 20 etc..

David



Jul 23 '05 #11

P: n/a
Good point, but the sort for an image ending in an alphanumeric character
would then be sorted alphanumerically so it isn't an issue, and it will sort them properly "automatically".


I should have said it will sort them properly "except" for the "last"
existing numbers in the image name, "if" these numbers are 1, 10 etc..
That's where the problem lies, in these "last" numbers in the image name.

David
Jul 23 '05 #12

P: n/a
Lee
David said:
And what about "darkimage10a.jpg" and "darkimage10b.jpg"?
And what about the user who likes Roman Numerals?

Good point, but the sort for an image ending in an alphanumeric character
would then be sorted alphanumerically so it isn't an issue, and it will sort
them properly "automatically".

It's just the numbers that appear last in the image name with reference to
1, 10, 2, 20 etc..


You need to put more thought into this.

alpha1
alpha2
alpha2a
alpha3
alpha10
alpha10a
alphabet1
alphabet2

Jul 23 '05 #13

P: n/a
David wrote:
Good point, but the sort for an image ending in an alphanumeric character would then be sorted alphanumerically so it isn't an issue, and it
will sort
them properly "automatically".
I should have said it will sort them properly "except" for the "last"
existing numbers in the image name, "if" these numbers are 1, 10

etc.. That's where the problem lies, in these "last" numbers in the image name.
David


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

function sortPix(arr)
{
var temp = arr.sort();
return temp.sort(
function(a, b)
{
var aa, bb;
if ((aa = a.replace(/\d/g, '')) == (bb = b.replace(/\d/g, '')))
return a.match(/\d+/) - b.match(/\d+/);
else return aa > bb;
}
);
}

var pix =
[
'hot_potato4.jpg' ,
'lightimage1.jpg' ,
'darkimage10b.jpg' ,
'foo_pix4.png' ,
'darkimage1.jpg' ,
'darkimage10.png' ,
'darkimage1a.jpg' ,
'blueimage2.jpg' ,
'image22.gif' ,
'darkimage1b.jpg' ,
'bigpic.jpeg' ,
'red_img.gif' ,
'hot_potato22.jpg' ,
'hot_potato2.jpg' ,
'foo_pix3.png' ,
'darkimage10a.jpg'
];

var sorted = sortPix(pix);
alert(sorted.join('\n'));

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

For some reason this doesn't work in FF, just ie6win. No idea why.
Is this the desired sort? Can we do this with php? #:-)

Jul 23 '05 #14

P: n/a
"David" <ri***@dd.com> writes:
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,ligh timage1.jpg It sorts the image names alphanumerically 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?


Everything is possible, if you can specify the desired outcome clearly.

A very general method for comparing strings containing numbers is
numCmp here:
---
function cmp(a,b) { // standard comparison.
return (b<a)-(a<b);
}

function numCmp(a,b) {
var re1 = /(\d+)|\D+/g;
var re2 = /(\d+)|\D+/g;
re1.lastIndex = 0;re2.lastIndex=0; // Opera 8 bug.
var res = 0;
do {
match1 = re1.exec(a);
match2 = re2.exec(b);
if (match1) {
if (match2) {
if (match1[1]) {
if (match2[1]) { // fully numeric.
res = Number(match1[1]) - Number(match2[1]) || cmp(match1[0],match2[0]);
} else {
res = -1;
}
} else {
if (match2[1]) {
res = 1;
} else {
res = cmp(match1[0],match2[0]);
}
}
} else {
res = 1;
}
} else {
if (match2) {
res = -1;
} else {
res = 0; break;
}
}
} while (res == 0);
return res;
}

---

It splits the strings into blocks of digits and non-digits respectively,
and then compares the digit-blocks as numbers. More precisely, it sorts
as if each digit block was one character that comes before any normal
character. If the numbers are identical, their string representation is
compared normally to disambiguate, i.e., "ab01" comes before "ab1".

Example:
---
var l = ["ab10", "ab!", "ab2", "ab02", "ab0"];
var ls = l.sort(numCmp);
alert(ls); // ab0, ab02, ab2, ab10, ab!
---

It's not a very effective way to sort, since each comparison splits
both strings into blocks again. For efficiency, you could split the
strings into blocks first, so it's only done once per string, and then
sort using these arrays as keys.

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #15

P: n/a
That works pretty consistently. For me, I don't care the order in which the
sort comes out but I need it to be consistent with the naming convention
amongst what the server serves up and the application I am writing. This
seems to do it.

Thanks to all that helped.

David

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:ll**********@hotpop.com...
"David" <ri***@dd.com> writes:
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,ligh timage1.jpg
It sorts the image names alphanumerically 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?


Everything is possible, if you can specify the desired outcome clearly.

A very general method for comparing strings containing numbers is
numCmp here:
---
function cmp(a,b) { // standard comparison.
return (b<a)-(a<b);
}

function numCmp(a,b) {
var re1 = /(\d+)|\D+/g;
var re2 = /(\d+)|\D+/g;
re1.lastIndex = 0;re2.lastIndex=0; // Opera 8 bug.
var res = 0;
do {
match1 = re1.exec(a);
match2 = re2.exec(b);
if (match1) {
if (match2) {
if (match1[1]) {
if (match2[1]) { // fully numeric.
res = Number(match1[1]) - Number(match2[1]) ||

cmp(match1[0],match2[0]); } else {
res = -1;
}
} else {
if (match2[1]) {
res = 1;
} else {
res = cmp(match1[0],match2[0]);
}
}
} else {
res = 1;
}
} else {
if (match2) {
res = -1;
} else {
res = 0; break;
}
}
} while (res == 0);
return res;
}

---

It splits the strings into blocks of digits and non-digits respectively,
and then compares the digit-blocks as numbers. More precisely, it sorts
as if each digit block was one character that comes before any normal
character. If the numbers are identical, their string representation is
compared normally to disambiguate, i.e., "ab01" comes before "ab1".

Example:
---
var l = ["ab10", "ab!", "ab2", "ab02", "ab0"];
var ls = l.sort(numCmp);
alert(ls); // ab0, ab02, ab2, ab10, ab!
---

It's not a very effective way to sort, since each comparison splits
both strings into blocks again. For efficiency, you could split the
strings into blocks first, so it's only done once per string, and then
sort using these arrays as keys.

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 23 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.