Sort version-dotted number strings in Javascript?

前端 未结 14 1725
栀梦
栀梦 2020-12-05 15:01

I have an array of following strings:

[\'5.5.1\', \'4.21.0\', \'4.22.0\', \'6.1.0\', \'5.1.0\', \'4.5.0\'] 

...etc.

I need a soluti

14条回答
  •  [愿得一人]
    2020-12-05 15:19

    This solution accounts for version numbers that might not be in the full, 3-part format (for example, if one of the version numbers is just 2 or 2.0 or 0.1, etc).

    The custom sort function I wrote is probably mostly what you're looking for, it just needs an array of objects in the format {"major":X, "minor":X, "revision":X}:

    var versionArr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
    var versionObjectArr = [];
    var finalVersionArr = [];
    /*
    split each version number string by the '.' and separate them in an
    object by part (major, minor, & revision).  If version number is not
    already in full, 3-part format, -1 will represent that part of the
    version number that didn't exist.  Push the object into an array that
    can be sorted.
    */
    for(var i = 0; i < versionArr.length; i++){
      var splitVersionNum = versionArr[i].split('.');
      var versionObj = {};
      switch(splitVersionNum.length){
        case 1:
          versionObj = {
            "major":parseInt(splitVersionNum[0]),
            "minor":-1,
            "revision":-1
          };
          break;
        case 2:
          versionObj = {
            "major":parseInt(splitVersionNum[0]),
            "minor":parseInt(splitVersionNum[1]),
            "revision":-1
          };
          break;
        case 3:
          versionObj = {
            "major":parseInt(splitVersionNum[0]),
            "minor":parseInt(splitVersionNum[1]),
            "revision":parseInt(splitVersionNum[2])
          };
      }
      versionObjectArr.push(versionObj);
    }
    
    //sort objects by parts, going from major to minor to revision number.
    versionObjectArr.sort(function(a, b){
      if(a.major < b.major) return -1;
      else if(a.major > b.major) return 1;
      else {
        if(a.minor < b.minor) return -1;
        else if(a.minor > b.minor) return 1;
        else {
          if(a.revision < b.revision) return -1;
          else if(a.revision > b.revision) return 1;
        }
      }
    });
    
    /*
    loops through sorted object array to recombine it's version keys to match the original string's value.  If any trailing parts of the version
    number are less than 0 (i.e. they didn't exist so we replaced them with
    -1) then leave that part of the version number string blank. 
    */
    for(var i = 0; i < versionObjectArr.length; i++){
      var versionStr = "";
      for(var key in versionObjectArr[i]){
        versionStr = versionObjectArr[i].major;
        versionStr += (versionObjectArr[i].minor < 0 ? '' : "." + versionObjectArr[i].minor);
        versionStr += (versionObjectArr[i].revision < 0 ? '' : "." + versionObjectArr[i].revision);
      }
      finalVersionArr.push(versionStr);
    }
    console.log('Original Array: ',versionArr);
    console.log('Expected Output: ',['4.5.0', '4.21.0', '4.22.0', '5.1.0', '5.5.1', '6.1.0']);
    console.log('Actual Output: ', finalVersionArr);

提交回复
热议问题