473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Function that compares 2 alpha-numeric values?

Does anyone have a code snippet to compare those values so I can sort
the array of alpha-numeric values that include both characters and
integers in it?

I.e., if we have values like 4236 and 123234, I want 4236 to be second
because 4 is bigger than 1 rather than using the numeric comparison.
The strings can include character values and strings. Basically, I have
the bubble sort function, the question is how to compare those types of
strings in the alpha-numeric order.

i.e.,

A83745
B34974
127734
34456
788

I looked all over the web thinking that this simple question would be
answered somewhere, but could not find the answer. Please help.

Thanks!

Jul 23 '05 #1
3 6487
VK
su********@yahoo.com wrote:
Does anyone have a code snippet to compare those values so I can sort
the array of alpha-numeric values that include both characters and
integers in it?

I.e., if we have values like 4236 and 123234, I want 4236 to be second
because 4 is bigger than 1 rather than using the numeric comparison.
The strings can include character values and strings. Basically, I have
the bubble sort function, the question is how to compare those types of
strings in the alpha-numeric order.

i.e.,

A83745
B34974
127734
34456
788

I looked all over the web thinking that this simple question would be

The time I wrote this script (a while ago) I was crying for Perl with
his "cmp" and shuttle <=> Get's really "if'y" in JavaScript w/o them
:-)
function alfanum(a,b) {
var result = 0;
if ((typeof(a)=='number')&&(typeof(b)=='number')) {
result = a-b;
}
else if ((typeof(a)=='string')&&(typeof(b)=='string')) {
if (a < b) {result = -1;}
else if (a > b) {result = 1;}
else {result = 0;}
}
else if (typeof(a)=='string'){
result = 1;
}
else {
result = -1
}
return result;
}

var arr = [9,'A',8,'B',7,'C',6,'D',5,'E',4,'F',3,'G',2,'H',1];

alert(arr.sort(alfanum));

Jul 23 '05 #2
JRS: In article <11**********************@g44g2000cwa.googlegroups .com>
, dated Thu, 21 Jul 2005 08:42:47, seen in news:comp.lang.javascript,
su********@yahoo.com posted :
Does anyone have a code snippet to compare those values so I can sort
the array of alpha-numeric values that include both characters and
integers in it?

I.e., if we have values like 4236 and 123234, I want 4236 to be second
because 4 is bigger than 1 rather than using the numeric comparison.
The strings can include character values and strings. Basically, I have
the bubble sort function, the question is how to compare those types of
strings in the alpha-numeric order.

i.e.,

A83745
B34974
127734
34456
788

I looked all over the web thinking that this simple question would be
answered somewhere, but could not find the answer. Please help.


As you've looked all over the Web I assume that there is no need to
repeat what you found on my Web site.

The default array sort would be to treat letters as bigger than digits;
no added code would be needed. That would give 12 34 78 A8 B3. You
don't need to write and download a sort algorithm. Use that order if
you can.

How can a string include a string?

You need to do one of two things : write a comparison function for
array.sort to use, or transform your data so that it will sort in the
right order, de-transforming afterwards. Since array.sort on a list of
N items will call the sort function >0(N) times, you should transform
the data if N is large.

The simplest transform might be to take each character of a string in
turn (I assume less than 240 possibilities), look up its rank in a
priority list, encode that as two Hex characters after adding 16, pad
with a "character" 00, and append the original data so that de-
transformation is just a substring operation.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3


su********@yahoo.com wrote:
Does anyone have a code snippet to compare those values so I can sort
the array of alpha-numeric values that include both characters and
integers in it?

I.e., if we have values like 4236 and 123234, I want 4236 to be second
because 4 is bigger than 1 rather than using the numeric comparison.
The strings can include character values and strings. Basically, I have
the bubble sort function, the question is how to compare those types of
strings in the alpha-numeric order.

i.e.,

A83745
B34974
127734
34456
788

I looked all over the web thinking that this simple question would be
answered somewhere, but could not find the answer. Please help.

Thanks!


Try a loop over the whole set first to separate the numeric from the
alphas with isNan. Run a sort on the numbers array, then a sort on the
alpha array. Add the sort alpha array elements to the end of the
numeric array.

http://www.askblax.com

Jul 23 '05 #4

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

Similar topics

5
by: Diez B. Roggisch | last post by:
Hi, I've got code objects that are created from strings at runtime that look like this: def p_op1(_, args): _.add_dependency('p_op1') As you migth guess, p_op1 is supposed to become an...
2
by: _andrea.l | last post by:
I'd like to roder an array: $array $array $array I'd like to order $array by name or by numbre of by a function($array,$array) that return 1 or -1 if id1 is less tha id2 or viceversa... How...
15
by: Eirik | last post by:
This is a little function I wrote, inspired by the thread "Urgent HELP! required for Caesar Cipher PLEASE" $ cat /home/keisar/bin/c/ymse/rot13.h char rot13(char character) { int changed;...
4
by: orekinbck | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
1
by: Ben | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
12
by: kalculus | last post by:
Hello Can someone explain what the following code is doing? void *get_gp_value() { void **function_pointer = (void **)get_gp_value; return function_pointer; }
4
by: manstey | last post by:
Hi, Is there a neat way to write a function that can receive either a string or a list of strings, and then if it receives a string it manipulates that, otherwise it manipulates each string in...
7
by: Tina | last post by:
Dear all, I'm looking for a routine which calculates the Gamma Function for a complex valued variable. I'm using #include <complex> to work with complex numbers. I define a complex number...
26
by: tnowles00 | last post by:
Hi friend, what is the use of function pointer in c language and where it is useful? tell with simple example...? plz help me.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.