我想按属性name
,
data.map(x => x.get("name"))
返回字符串,我想按名称按字母顺序对地图进行排序。
怎么做?我试过了:
return data.sortBy((val) => {
if (dir === "up") {
return val.get("name");
} else {
return - val.get("name");
}
});
答案 0 :(得分:29)
var fiends = Immutable.fromJS([{name: 'Squirrel'}, {name: 'Cat'}, {name: 'Raccoon'}]);
var sorted = fiends.sortBy(
(f) => f.get('name')
);
sorted.map(x => x.get('name')).toJS(); // ["Cat", "Raccoon", "Squirrel"]
使用localCompare:
fiends.sort(
(a, b) => a.get('name').localeCompare(b.get('name'))
);