stable sort in table

http://jsperf.com/chrome-stable-sort
This commit is contained in:
afc163 2016-02-26 16:31:33 +08:00
parent 1e26289da7
commit 7ea67b6e37

View File

@ -136,19 +136,18 @@ let AntTable = React.createClass({
}
}
if (typeof column.sorter === 'function') {
sorter = function (...args) {
let result = column.sorter.apply(this, args);
if (sortOrder === 'ascend') {
return result;
} else if (sortOrder === 'descend') {
return -result;
sorter = (a, b) => {
let result = column.sorter(a, b);
if (result !== 0) {
return (sortOrder === 'descend') ? -result : result;
}
return a.index - b.index;
};
}
const newState = {
sortOrder,
sortColumn,
sorter
sorter,
};
this.setState(newState);
this.props.onChange.apply(this, this.prepareParamsArguments(
@ -495,9 +494,7 @@ let AntTable = React.createClass({
},
findColumn(myKey) {
return this.props.columns.filter((c) => {
return this.getColumnKey(c) === myKey;
})[0];
return this.props.columns.filter(c => this.getColumnKey(c) === myKey)[0];
},
getCurrentPageData(dataSource) {
@ -530,6 +527,10 @@ let AntTable = React.createClass({
let data = dataSource || this.props.dataSource;
//
if (state.sortOrder && state.sorter) {
data = data.slice(0);
for (let i = 0; i < data.length; i++) {
data[i].index = i;
}
data = data.sort(state.sorter);
}
//