javascript 按字母顺序排序 immutable.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32564697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:28:29  来源:igfitidea点击:

Sorting by alphabetical order immutable.js

javascriptimmutabilityimmutable.js

提问by lipenco

I would like to sort immutable.js orderedList by property name,

我想按属性对 immutable.jsorderedList 进行排序name

data.map(x => x.get("name"))returns the string, I want to sort my map by name in alphabetical order.

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

How to do that? I tried:

怎么做?我试过:

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

回答by Luqmaan

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"]

With localCompare:

使用localCompare

fiends.sort(
  (a, b) => a.get('name').localeCompare(b.get('name'))
);