1

I have 2 strings in javascript that contain numeric numbers which represent file version.

i.e

var str1 = "11.11.1111.1111"
var str2 = "11.22.3333.5555"

How can i perform numeric comparison between them? I would like to check if str1 > str2

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
user829174
  • 6,132
  • 24
  • 75
  • 125
  • do you mind if `10.0.0.0` sorts between `1.0.0.0` and `2.0.0.0`? If you don't just do `str1 > str2` – John Dvorak Jun 06 '13 at 06:17
  • You must first also validate whether the strings are of required types and then perform comparison operations.. – AurA Jun 06 '13 at 06:19

2 Answers2

5

As long as str1 and str2 are guaranteed to be the same length and format, just do str1 > str2. String comparison is lexicographical (compare the first character of both, the second character of both... and return the value of the first difference), and for two equal length numbers/number-like strings that gives you exactly what you want.

If they are not guaranteed to be the same, try splitting by ., converting each string part to int and comparing them int-wise one by one until you find a difference.

Community
  • 1
  • 1
Patashu
  • 21,443
  • 3
  • 45
  • 53
  • "try splitting by ., casting each part you get back to int and comparing them int-wise one by one until you find a difference" -- Is there a lib for that? – John Dvorak Jun 06 '13 at 06:18
  • @Jan Dvorak It's a built-in function of Javascript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split For casting string to int: http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript – Patashu Jun 06 '13 at 06:20
  • I mean, is there a library that already does all of "splitting by `.`, casting each part you get back to int and comparing them int-wise one by one until you find a difference" in one call? – John Dvorak Jun 06 '13 at 06:23
  • @Jan Dvorak Why would one exist? – Patashu Jun 06 '13 at 06:27
  • It's a common operation so I guess someone must have already implemented it into their library. – John Dvorak Jun 06 '13 at 06:27
1

You could go for:

var str1 = '11.11.1111.1111'
   ,str2 = '11.22.3333.5555'
   ,compare = str1.split('.')
               .map(function(a) {
                    return +a > +(this.splice(0,1));
                },
                str2.split('.'))
              .reduce(function(a,b){return a||b;});
 //=> compare now: false 
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Replaced the answer with another approach – KooiInc Jun 06 '13 at 06:50
  • I can't see how it should work. What is the intention behind the `reduce` step? You should only accound for the most major version that they differ in, not count every place where they differ. The same for the other argument. It seems to me that replacing the reduce step iterator with `function(a,b){return a||b}` would act as desired. Also what's the point of `+(+a)` as opposed to `+a`? – John Dvorak Jun 06 '13 at 07:04
  • Also, why not use `unshift` instead of `splice`? – John Dvorak Jun 06 '13 at 07:05
  • `unshift` is non destructive. `a||b` is better, yep. `+(+a)` is a slip of the keyboard so to speak. – KooiInc Jun 06 '13 at 07:17
  • `unshift` _is_ destructive. I meant [`shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift), however, which _is_ destructive too. – John Dvorak Jun 06 '13 at 07:37