const versions = [‘1.10.2’, ‘1.3’, ‘2.0.1’, ‘1.5.1’, ‘1.5’, ‘1.4.1’]; 进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 测试数据集(无歧义版本号,所有版本号格式统一)
const testCases = [
{
input: ['10.0', '2.0', '1.9', '1.10', '1.8'],
expected: ['1.8', '1.9', '1.10', '2.0', '10.0'],
description: "主要版本不一致的情况,测试两位版本号的比较"
},
{
input: ['1.10', '1.3', '2.0', '1.5', '1.4'],
expected: ['1.3', '1.4', '1.5', '1.10', '2.0'],
description: "包含主版本和次版本,避免歧义"
},
{
input: ['1.0', '1.1', '1.0.1', '1.2', '1.0.0'],
expected: ['1.0', '1.0.0', '1.0.1', '1.1', '1.2'],
description: "包含固定格式版本,补位0"
},
{
input: ['2.1', '2.0', '1.9', '1.10', '1.10.1'],
expected: ['1.9', '1.10', '1.10.1', '2.0', '2.1'],
description: "小版本和补丁更新及主版本升级"
},
{
input: ['0.1', '0.0.1', '0.10', '0.2', '0.2.0'],
expected: ['0.0.1', '0.1', '0.2', '0.2.0', '0.10'],
description: "补位和次版本,不存在单一数字的歧义"
}
];

/**
* 用户需要在这里完成 sortVersions 函数
*/
function sortVersions(arr) {
let newArr = arr.map(e => e.split('.').map(Number));
newArr.sort((a, b) => {
for(let i = 0; i < 3; i++){
if(a[i] != b[i]){
return a[i] - b[i]
}
}
return 0
});
return newArr.map(e => e.join('.'));
}

/**
* 判题系统:检查用户的排序结果是否符合预期
*/
function judge() {
testCases.forEach(({ input, expected, description }, index) => {
const userSortedVersions = sortVersions([...input]); // 不改变原数组
const result = userSortedVersions.join(',') === expected.join(',') ? 'Accepted' : 'Wrong Answer';

console.log(`Test Case ${index + 1}: ${description}`);
console.log("Input Versions: ", input);
console.log("User Sorted Versions: ", userSortedVersions);
console.log("Expected Result: ", expected);
console.log("Judge Result: ", result);
console.log("-------------");
});
}

// 运行判题系统
judge();