HTML5¼¼Êõ

versionCompare °æ±¾ºÅ±È½Ï¹¤¾ß - СС²×º£

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-10-29 14:00 ÎÒÒªÆÀÂÛ( )

versionCompare °æ±¾ºÅ±È½Ï¹¤¾ß ¼ò½é ÐèÇó·Ç³£¼òµ¥£¬ÐèÒª±È½ÏÈí¼þ»òappµÄ°æ±¾ºÅ£¬ÅжϴóС£¬ÐÎÈç 0.10.2ÐÎʽµÄ°æ±¾ºÅ×Ö·û´®¡£ ʵÏÖÂß¼­ÊÇ°´Õյ㣨.£©·Ö¸î×Ö·û´®£¬È»ºóÖ𼶱Ƚϰ汾´ó

versionCompare °æ±¾ºÅ±È½Ï¹¤¾ß

¼ò½é

ÐèÇó·Ç³£¼òµ¥£¬ÐèÒª±È½ÏÈí¼þ»òappµÄ°æ±¾ºÅ£¬ÅжϴóС£¬ÐÎÈç 0.10.2ÐÎʽµÄ°æ±¾ºÅ×Ö·û´®¡£
ʵÏÖÂß¼­ÊÇ°´Õյ㣨.£©·Ö¸î×Ö·û´®£¬È»ºóÖ𼶱Ƚϰ汾´óС¡£
²»´æÔڵİ´0´¦Àí£¬¿Õ×Ö·û´®Ð¡ÓÚ·Ç¿Õ×Ö·û´®¡£

²âÊÔ½á¹û

'1' = '1'

'1' = '1.0'

'1' = '1.0.0'

'1.0' = '1'

'1.0' = '1.0'

'1.0.0' = '1.0'

'1.1' > '1'

'1.1' < '1.1.1'

'1.1' = '1.1.0'

'1.1.0' = '1.1'

'0.1.2' < '0.1.2.3'

'0.1.2' < '0.1.3'

'0.1.2' < '0.2.2'

'0.1.2' < '1.1.2'

'10.20.30' = '10.20.30'

'10.20.30' < '10.20.30.0.0.0.1'

'0.10.20.30' < '10.20.30'

'1' > ''

'' < '1'

'' < '0'

'' = ''

Ô´Âë /** * ¶Ô±È×Ö·û´®°æ±¾ºÅµÄ´óС£¬·µ»Ø1Ôòv1´óÓÚv2£¬·µ»Ø-1Ôòv1СÓÚv2£¬·µ»Ø0Ôòv1µÈÓÚv2 * * @author xxcanghai@²©¿ÍÔ° * @param {string} v1 Òª½øÐбȽϵİ汾ºÅ1 * @param {string} v2 Òª½øÐбȽϵİ汾ºÅ2 * @returns */ function versionCompare(v1, v2) { var GTR = 1; //´óÓÚ var LSS = -1; //СÓÚ var EQU = 0; //µÈÓÚ var v1arr = String(v1).split(".").map(function (a) { return parseInt(a); }); var v2arr = String(v2).split(".").map(function (a) { return parseInt(a); }); var arrLen = Math.max(v1arr.length, v2arr.length); var result; //Åųý´íÎóµ÷Óà if (v1 == undefined || v2 == undefined) { throw new Error(); } //¼ì²é¿Õ×Ö·û´®£¬ÈκηǿÕ×Ö·û´®¶¼´óÓÚ¿Õ×Ö·û´® if (v1.length == 0 && v2.length == 0) { return EQU; } else if (v1.length == 0) { return LSS; } else if (v2.length == 0) { return GTR; } //Ñ­»·±È½Ï°æ±¾ºÅ for (var i = 0; i < arrLen; i++) { result = xxcanghaiComp(v1arr[i], v2arr[i]); if (result == EQU) { continue; } else { break; } } return result; function xxcanghaiComp(n1, n2) { if (typeof n1 != "number") { n1 = 0; } if (typeof n2 != "number") { n2 = 0; } if (n1 > n2) { return GTR; } else if (n1 < n2) { return LSS; } else { return EQU; } } } ÔÚÏß²âÊÔÒ³Ãæ & Ô´ÂëÏÂÔØ

Demo:

ËùÓÐÔ´ÂëÏÂÔØGitHub:
https://github.com/xxcanghai/cnblogsFiles/tree/master/versionCompare

posted @

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • ¹¦ÄÜÆëÈ«²¢ÇұȽÏʱ÷ÖµÄJqueryͨÓÿªÔ´¿ò¼ÜÖ®¡¾ejq.js¡¿ - Ëï¿­Ðý

    ¹¦ÄÜÆëÈ«²¢ÇұȽÏʱ÷ÖµÄJqueryͨÓÿªÔ´¿ò¼ÜÖ®¡¾ejq.js¡¿ - Ëï¿­Ðý

    2016-08-04 18:00

  • DDD CQRS¼Ü¹¹ºÍ´«Í³¼Ü¹¹µÄÓÅȱµã±È½Ï - netfocus

    DDD CQRS¼Ü¹¹ºÍ´«Í³¼Ü¹¹µÄÓÅȱµã±È½Ï - netfocus

    2016-02-09 17:00

  • Java±È½ÏC#ʤÔںδ¦ - SolidMango

    Java±È½ÏC#ʤÔںδ¦ - SolidMango

    2015-11-06 08:56

  • ±ÏÒµÒ»Ä꣨Õâ¸ö×ܽáÀ´µÄ±È½ÏÍí£© - Ö»ºÈÅ£Ä̵ÄɱÊÖ

    ±ÏÒµÒ»Ä꣨Õâ¸ö×ܽáÀ´µÄ±È½ÏÍí£© - Ö»ºÈÅ£Ä̵ÄɱÊÖ

    2015-10-08 08:20

ÍøÓѵãÆÀ
Ô