| Total Complexity | 10 |
| Complexity/F | 2 |
| Lines of Code | 43 |
| Function Count | 5 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | class Sorter { |
||
| 2 | constructor(key, direction) { |
||
| 3 | this.key = key; |
||
| 4 | this.moveDirection = direction === 'desc' ? -1 : 1; |
||
| 5 | } |
||
| 6 | |||
| 7 | order(original) { |
||
| 8 | return original.sort(this.compare.bind(this)); |
||
| 9 | } |
||
| 10 | |||
| 11 | compare(firstElement, secondElement) { |
||
| 12 | let nameA = firstElement[this.key]; |
||
| 13 | let nameB = secondElement[this.key]; |
||
| 14 | |||
| 15 | if (typeof nameA === 'string') { |
||
| 16 | nameA = nameA.toUpperCase(); |
||
| 17 | } |
||
| 18 | |||
| 19 | if (typeof nameB === 'string') { |
||
| 20 | nameB = nameB.toUpperCase(); |
||
| 21 | } |
||
| 22 | |||
| 23 | if (nameA < nameB) { |
||
| 24 | return -1 * this.moveDirection; |
||
| 25 | } |
||
| 26 | |||
| 27 | if (nameA > nameB) { |
||
| 28 | return 1 * this.moveDirection; |
||
| 29 | } |
||
| 30 | |||
| 31 | return 0; |
||
| 32 | } |
||
| 33 | |||
| 34 | static create(original, key, direction) { |
||
| 35 | const sorter = new Sorter(key, direction); |
||
| 36 | |||
| 37 | return sorter.order(original); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | export default function multisort(original, key, direction) { |
||
| 42 | return Sorter.create(original, key, direction); |
||
| 43 | } |
||
| 44 |