按字母顺序排序immutable.js

时间:2015-09-14 12:22:18

标签: javascript immutability immutable.js

我想按属性name

对immutable.js orderedList进行排序

data.map(x => x.get("name"))返回字符串,我想按名称按字母顺序对地图进行排序。

怎么做?我试过了:

return data.sortBy((val) => {
    if (dir === "up") {
      return val.get("name");
    } else {
      return - val.get("name");
    }
  });

1 个答案:

答案 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'))
);