473,602 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6516
VK
su********@yaho o.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)=='n umber')&&(typeo f(b)=='number') ) {
result = a-b;
}
else if ((typeof(a)=='s tring')&&(typeo f(b)=='string') ) {
if (a < b) {result = -1;}
else if (a > b) {result = 1;}
else {result = 0;}
}
else if (typeof(a)=='st ring'){
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************ **********@g44g 2000cwa.googleg roups.com>
, dated Thu, 21 Jul 2005 08:42:47, seen in news:comp.lang. javascript,
su********@yaho o.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.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 #3


su********@yaho o.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
1966
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 instance method. Now how do I make this code object a function-object that can be set into the
2
1378
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 can I do that? How can I order in a complex case like this?
15
4479
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; changed = character - 'a' + 'n'; return changed;
4
1737
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 properties are equal. It seems to work OK except when a object has a property that is an array. The GetValue method of the PropertyInfo class returns an object, and I want to convert it to an array of objects. For some reason, this
1
1463
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 properties are equal. It seems to work OK except when a object has a property that is an array. The GetValue method of the PropertyInfo class returns an object, and I want to convert it to an array of objects. For some reason, this
29
3336
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 'atan2( )' Also, is there any function to get the round value, similar with floor and ceil, such like: round(3.1) = 3 round(3.6) = 4
12
1993
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
2275
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 the list? That is, rather than having to send a list of one member MyFunction(), I can send MyFunction('var1') or MyFunction()
7
8601
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 as complex <doublealpha(3.0, 1.0)
26
25578
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
7993
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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,...
1
8054
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,...
0
8268
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3900
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.