473,545 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting alphanumeric

Bob
Sorting the following alphanumerics using myArray.sort():

04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001

Results in:

04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
^

I cannot assume a standard format nor can I assume what alphanumeric
characters might be in the array. How do I sort this accuratly when
these values could contain any alpha numeric character? Is this
possible without creating a crazy hierarchy or characters and exception
rules??

Thanks.
Bob - Cerner Corp.

Jul 23 '05 #1
10 3629
Bob wrote on 08 dec 2004 in comp.lang.javas cript:
Sorting the following alphanumerics using myArray.sort():

04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001

Results in:

04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--


That is numeric sorting:

4-272-1 = -269
4-273-1 = -270
4-272-3 = -271

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

Jul 23 '05 #2
Bob wrote:
Sorting the following alphanumerics using myArray.sort():

04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001

Results in:

04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
^

I cannot assume a standard format nor can I assume what alphanumeric
characters might be in the array. How do I sort this accuratly when
these values could contain any alpha numeric character? Is this
possible without creating a crazy hierarchy or characters and exception
rules??


You'll have to roll your own sort function:
A= ["04-273-0001","04-272-0001","04-272-0003","04-272-0001","04-273-0001"]

function bobSort(a,b){
c=Number(a.spli t("-")[0])
d=Number(b.spli t("-")[0])
if (c==d){
c=Number(a.spli t("-")[1])
d=Number(b.spli t("-")[1])
}
if (c==d){
c=Number(a.spli t("-")[2])
d=Number(b.spli t("-")[2])
}
return c-d
}

alert(A.sort(bo bSort))

The following format may be superior, though:

c=parseInt(a.sp lit("-")[0],10)
d=parseInt(b.sp lit("-")[0],10)

....

Mick
Jul 23 '05 #3
Mick White wrote:
[...]
You'll have to roll your own sort function:

[...]

A good start Mick that got me thinking. I can't believe JavaScript
doesn't have a generic sort that works on alpha-numeric strings. So I
had a hack at your code and came up with what's below. My contribution
to the world is to kick off a generic sort function.

Whether numbers sort ahead of non-numbers can be modified to suit by
making all comparisons using ASCII codes or by changing the charCodeAt
lines slight to add or subtract a constant, or multiply by -1;

My modification of your script handles any format string. To handle
numbers and non-numbers, I change non-numbers to their ASCII code and
compare that to single digits. Not great, but it does sort OK - caveat
below.

If the sort runs out of characters to compare, it should put the
shortest one ahead of the longest. Different browsers require a
different return value: Safari needs -1, Firefox needs 0. I don't know
how to discriminate using feature detection - or should I be returning
something else?

Also, this causes a difference in the sort order for different browsers
(arggghh).

I got it this far, over to the gurus. An improvement would be to have
two sorts: sortAsNum() and sortAsChar().

Test results (all on Mac):
Safari: fine
Camino: fine
Firefox: need to change return -1 to return 0
IE 5.2: fails
Netscape: need to change return -1 to return 0
Opera: Sometimes needs -1, sometimes 0 depending on whether some
entries start with alphas or not.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><title>So rt play</title>
<script type="text/javascript">
function shortestOf(a,b) {
return (a.length <= b.length)? a.length:b.leng th;
}
function bobSort(a,b){
var z = shortestOf(a,b) ;
for (var i=0; i<shortestOf(a, b); i++) {
var c = a.split('')[i];
var d = b.split('')[i];

if ( c != d ) {
c = (isNaN(c))? c.charCodeAt(0) :c;
d = (isNaN(d))? d.charCodeAt(0) :d;
var x = c-d;
return c - d;
}

}
return -1;
}
function saySort(inp) {
var p = inp.split('\n') ;
alert(p);
alert(p.sort(bo bSort).join('\n '));
}
</script>

</HEAD>
<BODY>
<form action="">
<textarea cols="40" rows="20" name="stuff">04-273-0005
040-272-0001
040-272-0003
040-272-0001
04a-2y2-00c0
04-273-0001
04a-222-00a0
04a-222-00b0
04a-222-00b1
04z-2x2-00a0
04a-2x2-00a0
04a-2y2-00a0
04a-2y2-00c0
04a-2y2-00b0
04a
0ba-cc2&&8##y2-00b0
</textarea>
<input type="button" value="saySort( )" onclick="
saySort(this.fo rm.stuff.value) ;
"> <input type="reset">
</form>
</BODY>
</HTML>

--
Rob
Jul 23 '05 #4
RobG wrote:
Mick White wrote:
[...]
You'll have to roll your own sort function:
[...]

[snip]


<script type="text/javascript">
function shortestOf(a,b) {
return (a.length <= b.length)? a.length:b.leng th;
}
function bobSort(a,b){
var z = shortestOf(a,b) ;
for (var i=0; i<shortestOf(a, b); i++) {
var c = a.split('')[i];
var d = b.split('')[i];

if ( c != d ) {
c = (isNaN(c))? c.charCodeAt(0) :c;
d = (isNaN(d))? d.charCodeAt(0) :d;
var x = c-d;
return c - d;
}

}
return -1;
}


....
I didn't read the OP's post carefully, in which he did mention alpha
characters. I'll run some tests on your code, but it seems as if I have
the same battery of browsers as you do. What I've done in the past is to
isolate alphas from numeric:
http://www.mickweb.com/football/aleague/profiles.html (Power Ratings)
I use sortNumerical2( ) which assigns the variables text1 and text2 an
arbitary low number to non-numeric array entries. (c & d in the OP's case).

Mick

Jul 23 '05 #5
JRS: In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
dated Wed, 8 Dec 2004 11:52:38, seen in news:comp.lang. javascript, Bob
<bo*********@gm ail.com> posted :
Sorting the following alphanumerics using myArray.sort():

04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001

Results in:

04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
^

I cannot assume a standard format nor can I assume what alphanumeric
characters might be in the array. How do I sort this accuratly when
these values could contain any alpha numeric character? Is this
possible without creating a crazy hierarchy or characters and exception
rules??


You do not say whether your array has been loaded with strings or with
numerical expressions; that is most important. Neither do you say what
system(s) you are testing on.

As strings, they should sort lexically to :
04-272-0001,04-272-0001,04-272-0003,04-273-0001,04-273-0001
As expressions, by value to :
-269,-269,-270,-270,-271
The result you show should not, IMHO, be obtained.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
Mick White wrote:
RobG wrote:
Mick White wrote:
[...]
You'll have to roll your own sort function:

[...]

[snip]

<script type="text/javascript">
function shortestOf(a,b) {
return (a.length <= b.length)? a.length:b.leng th;
}
function bobSort(a,b){
var z = shortestOf(a,b) ;
for (var i=0; i<shortestOf(a, b); i++) {
var c = a.split('')[i];
var d = b.split('')[i];

if ( c != d ) {
c = (isNaN(c))? c.charCodeAt(0) :c;
d = (isNaN(d))? d.charCodeAt(0) :d;
var x = c-d;
return c - d;
}

}
return -1;
}


....
I didn't read the OP's post carefully, in which he did mention alpha
characters. I'll run some tests on your code, but it seems as if I have
the same battery of browsers as you do. What I've done in the past is to
isolate alphas from numeric:
http://www.mickweb.com/football/aleague/profiles.html (Power Ratings)
I use sortNumerical2( ) which assigns the variables text1 and text2 an
arbitary low number to non-numeric array entries. (c & d in the OP's case).


An interesting approach. BTW, if you make your DOB ISO8601, they will
sort much better (e.g. 01/21/1980 becomes 1980-10-21, a fairly trivial
conversion I think). The dates will then sort properly as either chars
or numbers (noting that all single digit numbers must be zero-padded to
two digits). You could use the conversion just for the sort, then put
them back as "US Dates" if that makes your users more comfortable.

A small fix to my routine is to change:

return -1;

to

return (a.length-b.length);

The obvious error came to me whilst I was in the shower. It fixes the
"should I use -1 or zero" problem - I was using completely the wrong
logic. My only defence is that it was about midnight on a very long
day.

The lines:

var z = shortestOf(a,b) ;
....
var x = c-d;

can both be ditched, they are remnants of development & debug.

shortestOf is modified to return the shortest string, rather than its
length (that's more logical and useful I think).

Finally, the 'split character' can be passed to the function so that
the calling function can say what the delimiter is (comma, return, ...)

I have tested the new version in IE, Firefox and Netscape in Windows
and all is fine. So if the OP is still watching this thread, here is
a generic "I want to sort anything" routine:

<script type="text/javascript">
/* Returns the length of the shortest of two strings */
function shortestOf(a,b) {
return (a.length <= b.length)? a:b;
}

function bobSort(a,b){
// Only iterate for the length of the shortest string
for (var i=0; i<shortestOf(a, b).length; i++) {
var c = a.split('')[i];
var d = b.split('')[i];
// When we get to the first non-identical character,
// sort on it
if ( c != d ) {
c = (isNaN(c))? c.charCodeAt(0) :c;
d = (isNaN(d))? d.charCodeAt(0) :d;
return c-d;
}
}
// If get to the end of the shortest string
// and all evaluated chars are the same...
return (a.length-b.length);
}

/* inp is an string of values separated by splitChar */
function saySort(inp,spl itChar) {
// splitChar is the array delimiter
var p = inp.split(split Char);
alert(p.sort(bo bSort).join('\n '));
}
</script>

--
Rob
Jul 23 '05 #7
The saga continues...

I was a bit concerned over performance, 300 records takes about 4
seconds to sort on my machine in Firefox, so I did a bit of
optimisation and now 300 records take about 1 second.

Change bobSort to:

function bobSort(a,b){
// Only iterate for the length of the shortest string
var c = a.split('');
var d = b.split('');
for (var i=0; i<shortestOf(a, b).length; i++) {
// When we get to the first non-identical character,
// sort on it
if ( c[i] != d[i] ) {
c[i] = (isNaN(c[i]))? c[i].charCodeAt(0): c[i];
d[i] = (isNaN(d[i]))? d[i].charCodeAt(0): d[i];
return c[i]-d[i];
}
}
// If get to the end of the shortest string
// and all evaluated chars are the same...
return (a.length-b.length);
}

Making all comparisons on charCode makes almost zero difference to the
time taken (I thought it would be much quicker), but it does make the
if statement really simple:

if ( c[i] != d[i] ) {
return c[i].charCodeAt(0) - d[i].charCodeAt(0);
}

Choose whatever suits. And I'm done. ;-)

--
Rob
Jul 23 '05 #8
On Thu, 09 Dec 2004 23:53:27 GMT, RobG <rg***@iinet.ne t.auau> wrote:

I haven't been following this thread really - I've been kinda busy
elsewhere in this group - but I will contribute one thing...
I was a bit concerned over performance
[snip]

Well, one way to improve performance is to only call shortestOf once. At
the moment, it's called on *every* iteration of the loop.
for (var i=0; i<shortestOf(a, b).length; i++) {


for(var i = 0, n = shortestOf(a, b).length; i < n; ++i) {

or

for(var i = 0, n = Math.min(a.leng th, b.length); i < n; ++i) {

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
Michael Winter wrote:
[...]

for(var i = 0, n = shortestOf(a, b).length; i < n; ++i) {
About halved the execution time. My original code was actually almost
identical but I didn't realise how much it affects performance.
for(var i = 0, n = Math.min(a.leng th, b.length); i < n; ++i) {


Shaved another 10%. Firefox takes about 2.8 seconds for 1,200 records,
IE about 1.8 seconds. The original takes 25 seconds (or so...).

Times are for comparative purposes only.

Thanks Mike.

--
Rob
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1632
by: Help! | last post by:
Does any one knows out of the top of their heads if Windows has a command line or any numerical sorting utility -not alphabetical. I found some Java script that can do it,but want to make sure I am not reinventing the wheel.
7
3242
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
3
12321
by: Duy Lam | last post by:
I'm using collation SQL_Latin1_General_CP1_CI_AS and I need to sort a varchar field. There are some elements, however, that I want to come AFTER any alphabetic characters. Is there any character that sorts after "Z"? In normal ASCII, there are various characters that would sort after "Z" (e.g. "~"), but I can't seem to figure out what the case...
6
2959
by: Chuck M | last post by:
Is there a simple way to change the sort order in Access (numeric-alpha) to alphanumeric? I've searched the help files and some books, but found no answer. Thanx for any advice Chuck
1
1414
by: Steve Leferve | last post by:
Hey folks - I'm working on a database that has a questionaire. The questions belong to groups, categories, then sub-categories. These questions must follow a sorting order that doesn't have any built-in definition (i.e. alphabetically). What's the best way to do this kind of thing? It has to be user-editable.
1
5868
by: VMI | last post by:
How can I sort a table column correctly when the table has values like "0", "A1", "AA-1", "B21", "3C", 4-32A", "1", "11-1", 2-A", etc... The table will then be loaded to a grid and that's when it'll have to sort correctly (when clicking on the col. header). Windows Explorer (in XP) seems to do this correctly when I sort files with these...
2
2261
by: java_jazzy | last post by:
Hai , May I know how do we sort alphanumeric characters present in the same column. Thanking you, java_jazzy.
5
2558
by: JJM0926 | last post by:
I need some help with sorting some part numbers that are alphanumeric. I have a table with a part number field which are in the following format--sw1000, sw1200, sw2000, sw2600, sw3000, sw21000, sw30200 etc.. When I sort that field in the table I want them to be sorted in order chronologically starting with the lowest value to the highest...
4
2046
by: cpptutor2000 | last post by:
Could some C guru provide some hints on my problem? I am trying to sort an array of character strings, where each string contains lowercase, uppercase, digits as well as non-alphanumeric characters as '-', '(' or '/'. Obviously, standard C functions as 'strcmp' would fail in these cases. I can convert all the non-digit characters to...
0
7484
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
7415
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
7675
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
7928
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...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
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
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
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
0
726
bsmnconsultancy
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...

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.