Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/bootstrap-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ class BootstrapTable {
}

initSort () {
let name = this.options.sortName
const name = this.options.sortName
const order = this.options.sortOrder === 'desc' ? -1 : 1
const index = this.header.fields.indexOf(this.options.sortName)
let timeoutId = 0
Expand All @@ -496,31 +496,33 @@ class BootstrapTable {
}
})
}

if (this.options.customSort) {
Utils.calculateObjectValue(this.options, this.options.customSort, [
this.options.sortName,
this.options.sortOrder,
this.data
])
} else {
this.data.sort((a, b) => {
if (this.header.sortNames[index]) {
name = this.header.sortNames[index]
}
const aa = Utils.getItemField(a, name, this.options.escape)
const bb = Utils.getItemField(b, name, this.options.escape)
const value = Utils.calculateObjectValue(this.header, this.header.sorters[index], [aa, bb, a, b])
} else if (this.options.groupBy && this.options.groupByField !== '') {
const groupedData = {}

if (value !== undefined) {
if (this.options.sortStable && value === 0) {
return order * (a._position - b._position)
}
return order * value
this.data.forEach(item => {
const groupKey = Utils.getItemField(item, this.options.groupByField, this.options.escape)

if (!groupedData[groupKey]) {
groupedData[groupKey] = []
}
groupedData[groupKey].push(item)
})
const sortedGroups = Object.keys(groupedData).sort().map(groupKey => {
const group = groupedData[groupKey]

return Utils.sort(aa, bb, order, this.options, a._position, b._position)
Utils.sort(name, order, index, group, this.header, this.options)
return group
})

this.data = [].concat(...sortedGroups)
} else {
Utils.sort(name, order, index, this.data, this.header, this.options)
}

if (this.options.sortClass !== undefined) {
Expand Down
94 changes: 55 additions & 39 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,57 +514,73 @@ export default {
return data
},

sort (a, b, order, options, aPosition, bPosition) {
if (a === undefined || a === null) {
a = ''
}
if (b === undefined || b === null) {
b = ''
}
sort (name, order, index, data, header, options) {
data.sort((a, b) => {
if (header.sortNames[index]) {
name = header.sortNames[index]
}

if (options.sortStable && a === b) {
a = aPosition
b = bPosition
}
let aa = this.getItemField(a, name, options.escape)
let bb = this.getItemField(b, name, options.escape)
const value = this.calculateObjectValue(header, header.sorters[index], [aa, bb, a, b])

// If both values are numeric, do a numeric comparison
if (this.isNumeric(a) && this.isNumeric(b)) {
// Convert numerical values form string to float.
a = parseFloat(a)
b = parseFloat(b)
if (a < b) {
return order * -1
if (value !== undefined) {
if (options.sortStable && value === 0) {
return order * (a._position - b._position)
}
return order * value
}
if (a > b) {
return order

if (aa === undefined || aa === null) {
aa = ''
}
if (bb === undefined || bb === null) {
bb = ''
}
return 0
}

if (options.sortEmptyLast) {
if (a === '') {
return 1
if (options.sortStable && aa === bb) {
aa = a._position
bb = b._position
}

if (b === '') {
return -1
// If both values are numeric, do a numeric comparison
if (this.isNumeric(aa) && this.isNumeric(bb)) {
// Convert numerical values form string to float.
aa = parseFloat(aa)
bb = parseFloat(bb)
if (aa < bb) {
return order * -1
}
if (aa > bb) {
return order
}
return 0
}
}

if (a === b) {
return 0
}
if (options.sortEmptyLast) {
if (aa === '') {
return 1
}
if (bb === '') {
return -1
}
}

// If value is not a string, convert to string
if (typeof a !== 'string') {
a = a.toString()
}
if (aa === bb) {
return 0
}

if (a.localeCompare(b) === -1) {
return order * -1
}
// If value is not a string, convert to string
if (typeof aa !== 'string') {
aa = aa.toString()
}

if (aa.localeCompare(bb) === -1) {
return order * -1
}

return order
return order
})
},

getEventName (eventPrefix, id = '') {
Expand Down