Completed
Push — master ( b8c0d7...8cf4a8 )
by Pieter Epeüs
21s queued 10s
created

multisort.js ➔ multisort   B

Complexity

Conditions 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 8
c 0
b 0
f 0
cc 7
1
module.exports = function multisort(original, key, direction) {
2
    let moveDirection = 1;
3
4
    if (direction === 'desc') {
5
        moveDirection = -1;
6
    }
7
8
    return original.sort((a, b) => {
9
        let nameA = a[key];
10
        let nameB = b[key];
11
12
        if (typeof nameA === 'string') {
13
            nameA = nameA.toUpperCase();
14
        }
15
16
        if (typeof nameB === 'string') {
17
            nameB = nameB.toUpperCase();
18
        }
19
20
        if (nameA < nameB) {
21
            return -1 * moveDirection;
22
        }
23
24
        if (nameA > nameB) {
25
            return 1 * moveDirection;
26
        }
27
28
        return 0;
29
    });
30
};
31