473,473 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3177
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
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
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
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
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
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

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
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
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
> 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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.