forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
63 lines (58 loc) · 1.26 KB
/
sort.js
File metadata and controls
63 lines (58 loc) · 1.26 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
const sortMethod = {
string: (a, b) => a.localeCompare(b),
number: (a, b) => a - b,
date: (a, b) => (new Date(a)) - (new Date(b)),
boolean: (a, b) => {
if (a && !b) { return -1 }
if (!a && b) { return 1 }
return 0
}
}
function nextDirection (dir) {
if (dir === 0) { return 1 }
if (dir === 1) { return -1 }
return 0
}
function getSortFn (sort, type) {
if (typeof sort === 'function') {
return sort
}
return sortMethod[type] || sortMethod.number
}
export default {
data () {
return {
sorting: {
field: '',
dir: 0,
fn: false
}
}
},
watch: {
'sorting.dir' () {
this.resetBody()
}
},
methods: {
setSortField (col) {
if (this.sorting.field === col.field) {
this.sorting.dir = nextDirection(this.sorting.dir)
if (this.sorting.dir === 0) {
this.sorting.field = ''
}
return
}
this.sorting.field = col.field
this.sorting.dir = 1
this.sorting.fn = getSortFn(col.sort, col.type)
},
sort (rows) {
let sortFn = this.sorting.fn || ((a, b) => a - b)
const
field = this.sorting.field,
dir = this.sorting.dir
rows.sort((a, b) => dir * sortFn(a[field], b[field]))
}
}
}