我有这个对象数组(snippit) - >来自我的工作人员的状态。
我按照HERE
获得的示例运行此操作但是在运行它时,它什么都不做,并且单步执行它表明a
和b
参数永远不会被初始化。
我真的不明白它是如何工作的。请你能解释一下发生的事情,而不会让我感到有些重复。我想了解这一点。
100:{mds_id: 291100176, mds_surname: "Kaal", mds_role: "Doctor", mds_grade: "CONS", mds_firstname: "", …}
101:{mds_id: 291100004, mds_surname: "Key", mds_role: "Nurse", mds_grade: "Clinical Nurse Specialist", mds_firstname: "Blogs", …}
102:{mds_id: 291100349, mds_surname: "Kendy", mds_role: "Doctor", mds_grade: "CONS", mds_firstname: "Fred", …}
103:{mds_id: 291100356, mds_surname: "Kin", mds_role: "Nurse", mds_grade: "", mds_firstname: "Roza", …}
104:{mds_id: 291100247, mds_surname: "Knit", mds_role: "Doctor", mds_grade: "CONS", mds_firstname: "", …}
getInitialState: function(){
var state = {
medicalstaffs:CustomizationStore.getMedicalstaffs(),
header: {
onClick: (field) => {
var staff = this.state.medicalstaffs;
var sortedStaff = staff.sort(this._compareValues(field, 'desc'));
this.setState({medicalstaffs: sortedStaff});
},
}
};
return util.joinObjects(state,this.updatedLists());
},
_compareValues: function(key, order='asc') {
return function(a, b) {
if(!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order == 'desc') ? (comparison * -1) : comparison
);
};
},
答案 0 :(得分:0)
我的解决方案
header: {
onClick: (column) => {
if(this.state.order === null || this.state.order === 'desc' ){
this.setState({order: 'asc'});
} else {
this.setState({order: 'desc'});
}
const order = this.state.order;
const key = column['property'];
var byName = this.state.medicalstaffs.slice(0);
byName.sort(function(a,b) {
const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order == 'desc') ? (comparison * -1) : comparison
);
});
this.setState({medicalstaffs: byName});
},
}