sort()

D

David

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
 
E

Evertjan.

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,image1.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,image111.jpg
 
P

Paul Cooper

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
 
E

Evertjan.

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.
 
R

RobB

David said:
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>
 
M

Mick White

Evertjan. said:
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,image1.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,image1.jpg"
alert(s.split(",").sort(compare).join("\n"))

Mick
 
D

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

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
 
D

David

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.jpg


Sorting them numericaly only will result in this..

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


There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,lightimage1.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
 
D

David

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,lightimage1.jpg


Sorting them numericaly only will result in this..

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


There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,lightimage1.jpg
 
L

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,lightimage1.jpg


Sorting them numericaly only will result in this..

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


There needs to be a way to sort them so the result is always like this..

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

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

David

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
 
D

David

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
 
L

Lee

David said:
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
 
R

RobB

David said:
will

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? #:)
 
L

Lasse Reichstein Nielsen

David said:
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,lightimage1.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
 
D

David

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 said:
David said:
There needs to be a way to sort them so the result is always like this..

blueimage2.jpg,darkimage1.jpg,darkimage10.jpg,lightimage1.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
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top