Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

比较版本号大小 #47

Open
nmsn opened this issue May 10, 2023 · 1 comment
Open

比较版本号大小 #47

nmsn opened this issue May 10, 2023 · 1 comment
Labels

Comments

@nmsn
Copy link
Contributor

nmsn commented May 10, 2023

题目描述:有一组版本号如下
['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']

现在需要对其进行排序,排序的结果为
['4.3.5','4.3.4.5','2.3.3','0.302.1','0.1.1']

@nmsn nmsn added the 算法 label May 10, 2023
@nmsn
Copy link
Contributor Author

nmsn commented May 10, 2023

arr.sort((a, b) => {
  let i = 0;
  const arr1 = a.split(".");
  const arr2 = b.split(".");

  while (true) {
    const s1 = arr1[i];
    const s2 = arr2[i];
    i++;
    if (s1 === undefined || s2 === undefined) {
      return arr2.length - arr1.length;
    }

    if (s1 === s2) continue;

    return s2 - s1;
  }
});
console.log(arr);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant