473,320 Members | 1,926 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,320 software developers and data experts.

string compare

shoonya
161 100+
according to javascript

Expand|Select|Wrap|Line Numbers
  1. 'a2'  < 'a9'
but

Expand|Select|Wrap|Line Numbers
  1. 'a8' > 'a10'
because it's comparing one character at a time moving from left right

do i need to write a separate string compare function or is there any alternatives available

shoonya
Jun 18 '07 #1
3 2934
shoonya
161 100+
i got this link but sting.compare is not working

Expand|Select|Wrap|Line Numbers
  1. http://www.webreference.com/js/column63/6.html
shoonya
Jun 18 '07 #2
mrhoo
428 256MB
You should be able to take what you need from this function-
it is meant to sort an array of strings that may contain digits,
and it collapses the 'number' parts to single values, rather than separate characters.

Expand|Select|Wrap|Line Numbers
  1. function naturalSort(a, b){
  2.     var cnt = 0, tem;
  3.     var a = a.toLowerCase();
  4.     var b = b.toLowerCase();
  5.     if (a == b) return 0;
  6.  
  7.     var rx = /^(\.)?\d/;
  8.     var L = Math.min(a.length, b.length) + 1;
  9.     while (cnt < L && a.charAt(cnt) === b.charAt(cnt) &&
  10.     rx.test(b.substring(cnt)) == false) cnt++;
  11.  
  12.     a = a.substring(cnt);
  13.     b = b.substring(cnt);
  14.  
  15.     if (rx.test(a) || rx.test(b)){
  16.         if (rx.test(a) == false) return a ? 1 : -1;
  17.         else if (rx.test(b) == false) return b ? -1 : 1;
  18.         else{
  19.             var tem = parseFloat(a) - parseFloat(b);
  20.             if (tem != 0) return tem;
  21.             else tem = a.search(/[^\.\d]/);
  22.             if (tem == -1) tem = b.search(/[^\.\d]/);
  23.             a = a.substring(tem);
  24.             b = b.substring(tem);
  25.         }
  26.     }
  27.     if (a == b) return 0;
  28.     return a > b ? 1 : -1;
  29. }
/*
test case:
var A='100.00,a101,b10,a10,b2,b,2,a5,a2'.split(',');

A.sort() returns [100.00,2,a10,a101,a2,a5,b,b10,b2]

A.sort(naturalSort) returns [2,100,100.00,a2,a5,a10,a101,b,b2,b10]

*/
Jun 18 '07 #3
shoonya
161 100+
Expand|Select|Wrap|Line Numbers
  1. function str_value(arg){
  2. var value=0;
  3. for(var k=0; k < parseInt(arg.length); k++ ){
  4.     value += parseInt(position_value(arg[k])) * Math.pow(10,parseInt(arg.length)-k-1)
  5.     }
  6. return value
  7. }
  8.  
  9. function position_value(chr){
  10. var alpha='0123556789abcdefghijklmnopqrstuvwxyz';
  11.  
  12. if(alpha.indexOf(chr)!=-1) return alpha.indexOf(chr)
  13.         else return '999'
  14. }
  15.  
i am using this piece of code for getting the value for any string

shoonya
Jun 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.