By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

string compare

shoonya
100+
P: 161
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
Share this Question
Share on Google+
3 Replies


shoonya
100+
P: 161
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

100+
P: 428
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
100+
P: 161
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

Post your reply

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