Code:
function compare(v1, v2, change) {
// The + converts to a number if possible
var itm1 = v1;
var itm2 = v2;
if (change) {
itm1 = +v1;
itm2 = +v2;
}
console.log("itm1:"+itm1+" itm2:"+itm2);
if (itm1 < itm2) { return -1; }
if (itm2 < itm1) { return 1; }
return 0;
}
one = "2";
two = "Two";
console.log("Test with +");
console.log("------");
console.log(compare(one, two, true));
console.log("");
console.log("Test without +");
console.log("------");
console.log(compare(one, two, false));
Output:
Test with +
------
itm1:2 itm2:NaN
0
Test without +
------
itm1:2 itm2:Two
-1