Hi all, A while back I asked how to sort an array of strings which would
have numerals and I wanted to put them in sequential numerical order.
For example:
myArray[0] = "file1";
myArray[1] = "file2";
myArray[2] = "file3";
myArray[3] = "file4";
myArray[4] = "file5";
myArray[5] = "file6";
myArray[6] = "file7";
myArray[7] = "file8";
myArray[8] = "file9";
myArray[9] = "file10";
myArray[10] = "file11";
etc...
However using the normal sort function below for numerical sorting...
---------------------------------------
function compare(a,b){
return(a-b)
}
myArray.sort(co mpare);
---------------------------------------
returns the array in an order I do not want and is not consistent.
For example:
myArray[0] = "file1";
myArray[1] = "file10";
myArray[2] = "file2";
myArray[3] = "file3";
myArray[4] = "file4";
etc..
Same thing happens when it gets to 20 etc..
I was given this code below which works great in most modern browsers but
crashes IE5, actually it pops an alert that the script has been running a
long time and wants me to bail. Can anyone shed some light as to why this
code fails or know of another way to accomplish this?
function cmp(a,b) {
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 ;
var res = 0;
do{ match1 = re1.exec(a);mat ch2 = re2.exec(b);
if(match1){if (match2){
if(match1[1]) {
if(match2[1]) {
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(mat ch2){res = -1;}else{res = 0; break;
}
}
} while(res == 0);
return res;
}
--
Thanks David 19 2629
David a écrit : Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order.
For example:
myArray[0] = "file1"; myArray[1] = "file2"; myArray[2] = "file3"; myArray[3] = "file4"; myArray[4] = "file5"; myArray[5] = "file6"; myArray[6] = "file7"; myArray[7] = "file8"; myArray[8] = "file9"; myArray[9] = "file10"; myArray[10] = "file11";
etc...
However using the normal sort function below for numerical sorting...
and ... why not rename your files ?
myArray[0] = "file01";
myArray[1] = "file02";
myArray[2] = "file03";
myArray[3] = "file04";
myArray[4] = "file05";
myArray[5] = "file06";
myArray[6] = "file07";
myArray[7] = "file08";
myArray[8] = "file09";
myArray[9] = "file10";
myArray[10] = "file11";
would fix your myArray.sort(co mpare)
--------------------------------------- function compare(a,b){ return(a-b) }
myArray.sort(co mpare); --------------------------------------- returns the array in an order I do not want and is not consistent.
For example: myArray[0] = "file1"; myArray[1] = "file10"; myArray[2] = "file2"; myArray[3] = "file3"; myArray[4] = "file4"; etc..
Same thing happens when it gets to 20 etc..
--
Stephane Moriaux et son [moins] vieux Mac
> and ... why not rename your files ?
Because they won't be my files and I will have no control over what they are
named, however they must be sorted in a specific order.
David
David <ri***@dd.com > wrote in message news:NhG_e.1146 9$qC4.1168@trnd dc02... Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order.
However using the normal sort function below for numerical sorting... --------------------------------------- function compare(a,b){ return(a-b) }
myArray.sort(co mpare); --------------------------------------- returns the array in an order I do not want and is not consistent.
For example: myArray[0] = "file1"; myArray[1] = "file10"; myArray[2] = "file2"; myArray[3] = "file3"; myArray[4] = "file4"; etc..
Same thing happens when it gets to 20 etc..
This routine compares the numerical value of the suffixes (if present).
var fNames=["file10","file1 ","file12","fil e123","fileNAN" ,"file5","file1 1","file3","fil e02"];
function bubbleSortOnSuf fix( f )
{
var didSwap=true,tm p,n1,n2;
while(didSwap)
for(var i=0, didSwap=false; i<f.length-1; i++)
{
if( isNaN( n1=parseInt( f[i].match( /\d+/ ) ) ) )
n1=0;
if( isNaN( n2=parseInt( f[i+1].match( /\d+/ ) ) ) )
n2=0;
if( n1 > n2 )
{
tmp=f[i];
f[i]=f[i+1];
f[i+1]=tmp;
didSwap=true;
}
}
}
bubbleSortOnSuf fix( fNames );
alert(fNames);
--
S.C.
David wrote: Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order.
For example:
myArray[0] = "file1"; myArray[1] = "file2"; myArray[2] = "file3"; myArray[3] = "file4"; myArray[4] = "file5"; myArray[5] = "file6"; myArray[6] = "file7"; myArray[7] = "file8"; myArray[8] = "file9"; myArray[9] = "file10"; myArray[10] = "file11";
etc...
However using the normal sort function below for numerical sorting... --------------------------------------- function compare(a,b){ return(a-b) }
myArray.sort(co mpare); --------------------------------------- returns the array in an order I do not want and is not consistent.
For example: myArray[0] = "file1"; myArray[1] = "file10"; myArray[2] = "file2"; myArray[3] = "file3"; myArray[4] = "file4"; etc..
Same thing happens when it gets to 20 etc..
I was given this code below which works great in most modern browsers but crashes IE5, actually it pops an alert that the script has been running a long time and wants me to bail. Can anyone shed some light as to why this code fails or know of another way to accomplish this?
Using a comparison function for sort was introduced with Navigator 3 and
you say that sort(cmp) works (though the result is not what you want) so
we can rule that out as the issue.
The logic in numCmp() is implemented using regular expressions, a much
more likely source of your (or rather IE 5's) problem.
Reduce the size of your array to say ['file1','file2' ,'file10'] just for
convenience and insert an alert in numCmp() that spits out the values of
re1 and re2 immediately after they are set - you may well find that IE5
barfs about there.
If it gets further, move the alert until you find out what the problem
is. Once you've done that, you know which bit of logic you have to
replace. I don't have IE 5 so can't test it for you.
What numCmp is doing is breaking the file names into arrays of digits
and non-digit parts, then compares them - chars as chars and numbers as
numbers. If one is a number and one a char, they are compared as chars.
The value of the first mis-match is returned - if no differences are
found, 0 is returned.
You can replicate the logic say using split and arrays, but it is
somewhat longer and more tedious to keep the bits in the right order.
As a quick and dirty solution, if the 'file' part is always identical
and only the number changes, then numCmp can be:
function numCmp2(a,b)
{
var x = a.replace(/\D+/g,'');
var y = b.replace(/\D+/g,'');
return cmp(+x, +y);
}
function cmp(a,b) { 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 ; var res = 0; do{ match1 = re1.exec(a);mat ch2 = re2.exec(b); if(match1){if (match2){ if(match1[1]) { if(match2[1]) { 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(mat ch2){res = -1;}else{res = 0; break; } } } while(res == 0); return res; }
-- Thanks David
--
Rob
Thanks Stephen. Your example works well and IE 5 isn't choking on it. I
think the problem is solved.
--
David
Thanks for the detailed explanation Rob. I will go through this function
with the alerts to see where the issue is. Stephens example works well and
IE5 seems to like it as well.
--
David
David wrote: Thanks for the detailed explanation Rob. I will go through this function with the alerts to see where the issue is. Stephens example works well and IE5 seems to like it as well.
-- David
Stephen's function does not seem to handle mixed digits and letters very
well, you might as well use the numCmp2() I posted.
Anyhow, below is aversion that implements the logic using arrays instead
of RegExp - actually it still uses a regular expression but not the same
way. I also removed the call to cmp() by including its functionality.
As far as I can tell it works exactly the same in Firefox and IE 6, can
you test it in IE 5 to see if it works?
<div id="xx"></div>
<script type="text/javascript">
var fileArray = [
"file02fin.jpg" ,"file01.jpg"," afile30.jpg","f ile30",
"file4.gif","5" ,"5.jpg","file6 .bat","file7"," zfile8","file9" ,
"fi6le10","file 11","1file20.jp g","file02","zf ile08","zfile8" ,
"file4.bat","fi le4"
];
function cmpNumStr(a, b)
{
// Turn a & b into arrays of digits & non-digit elements
var A = a.match(/\d+|\D+/g)
var B = b.match(/\d+|\D+/g)
var res = 0;
var i = 0;
do {
if(A[i]){
if(B[i]){ // Compare as numbers or chars if that fails
res = (A[i] - B[i]) || ((B[i]<A[i]) - (A[i]<B[i]));
} else {
return 1; // Theres an A element but no B, A wins
}
} else if (B[i]){
return -1; // Theres a B element but no A, B wins
} else {
return res; // Run out of elements, so even
}
i++;
} while ( res == 0 ); // Stop as soon non-equal elements found
return res;
}
var xx = document.getEle mentById('xx');
xx.innerHTML = '<b>Before:</b><br>' + fileArray.join( '<br>')
+ '<hr><b>After: </b><br>'
+ fileArray.sort( cmpNumStr).join ('<br>');
</script>
--
Rob
RobG wrote:
[...] As far as I can tell it works exactly the same in Firefox and IE 6, can you test it in IE 5 to see if it works?
It works in Safari 1.0.3 (Mac OS X 10.2.8) and IE 5.2 on Mac if that's
any help.
--
Rob As far as I can tell it works exactly the same in Firefox and IE 6, can you test it in IE 5 to see if it works?
It works in Safari 1.0.3 (Mac OS X 10.2.8) and IE 5.2 on Mac if that's any help.
Let me give it a whirl in the real world use here. I will let you know....
--
David This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David |
last post by:
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
|
by: alanbe |
last post by:
Greetings
I am making a flashcard type application to help me in my TCP/IP
protocols test.
My instructor will test us periodically on how a device or networking
function relates to the OSI layer. EG. bits-layer 1.
Any way, I want the quiz to reorder the problems each time I take it.
Here is part of the code i did so far for 62 components in the quiz.
|
by: yeti349 |
last post by:
Hi, I'm using the following code to retrieve data from an xml file and
populate a javascript array. The data is then displayed in html table
form. I would like to then be able to sort by each column. Once the
array elements are split, what is the best way to sort them? Thank you.
//populate data object with data from xml file.
//Data is a comma delimited list of values
var jsData = new Array();
jsData = {lib: "#field...
|
by: aarklon |
last post by:
Hi folks,
this is the program implementing quicksort
#include<stdio.h>
#include<stdlib.h>
#define swap(a,b){int t; t=a;a=b;b=t;}
int partition(int,int,int);
void quicksort(int,int,int);
|
by: Tony WONG |
last post by:
i have a number of forms with fax numbers to come up into arrays and then
combine to string.
after that i design the flow
1. break the string to array
now the string looks like this 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)
| |
by: ^cypis^ vel. SQ9JTI |
last post by:
Hi,
i need your help.
I have to prepare a homework, easy program, which will be sorting the
values from txt file and writing the solution to another txt file.
It has to be a bucket sort. Have anyone a source code for this sample?
Many thanks in advance!
Regards,
Luke
|
by: Tim Hunter |
last post by:
Hi I am using WinXP and Access 2003
Is it possible to store the field names of a table in an array and then
loop through the array and update the table using the field names stored
in the array? I can't figure out the coding to accomplish this.
I have an Excel application that is a monster and it has become too much
to maintain and test. I didn't write it but i support it. I am trying to
convert this application to Access and it is not as...
|
by: Randeh |
last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm sure there's more errors but I'll figure those out if I can just get it to compile.
#include <iostream>
#include <cstring>
using namespace std;
const int MaxNames = 21;
const int MaxChars = 16;
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |