Compare two Version numbers | Method to compare two versions . Returns 1 if v2 is smaller , - 1 if v1 is smaller , 0 if equal ; vnum stores each numeric part of version ; loop until both string are processed ; storing numeric part of version 1 in vnum1 ; storing numeric part of version 2 in vnum2 ; if equal , reset variables and go for next numeric part ; Driver method to check above comparison function
function versionCompare ( v1 , v2 ) { var vnum1 = 0 , vnum2 = 0 ; for ( var i = 0 , j = 0 ; ( i < v1 . length j < v2 . length ) ; ) { while ( i < v1 . length && v1 [ i ] != ' ' ) { vnum1 = vnum1 * 10 + ( v1 [ i ] - ' ' ) ; i ++ ; } while ( j < v2 . length && v2 [ j ] != ' ' ) { vnum2 = vnum2 * 10 + ( v2 [ j ] - ' ' ) ; j ++ ; } if ( vnum1 > vnum2 ) return 1 ; if ( vnum2 > vnum1 ) return - 1 ; vnum1 = vnum2 = 0 ; i ++ ; j ++ ; } return 0 ; } var version1 = " " ; var version2 = " " ; if ( versionCompare ( version1 , version2 ) < 0 ) document . write ( version1 + " " + " " ) ; else if ( versionCompare ( version1 , version2 ) > 0 ) document . write ( version2 + " " + " " ) ; else document . write ( " " + " " ) ;