473,560 Members | 2,959 Online
Bytes | Software Development & Data Engineering Community
+ 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 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>
<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.jo in('\n'));

</script>
</head>
<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"
alert(s.split(" ,").sort(compar e).join("\n"))

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"
alert(s.split(" ,").sort(compar e).join("\n"))

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

Similar topics

2
2004
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 lists (or tuples) in place by using a simple variant of the current idiom, i.e. list_of_lists.sort(*columns) where *columns is a tuple that...
1
2273
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, ascending = True): ' Sort lists or dictionaries by the specified key' t = type(container)
4
3730
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
4240
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 number of records. Both these numbers are known
7
2437
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 sort()". I'd heard you can do this within a namespace: namespace Fixed { using namespace std;
7
2432
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 Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here
48
4423
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should...
10
5582
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
16622
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 bubble sort(use when we have to 100 items: bubble sort is effective),shell sort is effective when one has to sort near abt 5000 items..selection sort is...
5
7801
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 columns, but I cannot figure out how to do that. I read somewhere that it is possible to do: but it does not work. Can anyone help me with this? PS:...
0
7632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7552
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7841
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8066
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7596
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6191
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3605
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2047
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1169
muto222
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.