-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortNamesByRomanNumerals.js
More file actions
71 lines (68 loc) · 1.73 KB
/
SortNamesByRomanNumerals.js
File metadata and controls
71 lines (68 loc) · 1.73 KB
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
65
66
67
68
69
70
71
function getSortedList(names) {
const namesObj = names.reduce((sum, curr) => {
const currentSum = sum;
const [name, order] = curr.split(' ');
if (!currentSum[name]) {
currentSum[name] = [];
}
currentSum[name].push(curr);
currentSum[name] = sortByRomanNumerals(currentSum[name]);
return currentSum;
}, {});
const sortedNames = Object.keys(namesObj);
sortedNames.sort((a, b) => {
if(a < b) { return -1; }
if(a > b) { return 1; }
return 0;
});
return sortedNames.map((o) => namesObj[o]).reduce((sum, ar) => {
return sum.concat(ar);
}, [])
}
function sortByRomanNumerals(array) {
const romanNumeralsOrderForReplace = [
'xl',
'l',
'xxx',
'xx',
'ix',
'x',
'iv',
'v',
'iii',
'ii',
'i',
];
const romanNumeralsMap = {
i: 1,
ii: 2,
iii: 3,
iv: 4,
v: 5,
ix: 9,
x: 10,
xx: 20,
xxx: 30,
xl: 40,
l: 50,
};
const arr = array.map((a) => {
const [name, number] = a.split(' ');
const obj = {
fullName: a,
name,
romanNumber: number,
};
let tempNumber = number.toLowerCase();
romanNumeralsOrderForReplace.forEach((num) => {
tempNumber = tempNumber.replace(num, `${romanNumeralsMap[num]},`);
});
const tempNumberArray = tempNumber.split(',');
const newNumber = tempNumberArray.reduce((s, c) => {
return s + (parseInt(c) ? parseInt(c) : 0);
}, 0)
obj.number = newNumber;
return obj;
}).sort((a, b) => a.number - b.number);
return arr.map((obj) => obj.fullName);
}