Sorting by alphabetical order immutable.js

23,803
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:

fiends.sort(
  (a, b) => a.get('name').localeCompare(b.get('name'))
);
Share:
23,803
lipenco
Author by

lipenco

Updated on May 26, 2020

Comments

  • lipenco
    lipenco almost 4 years

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

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

    How to do that? I tried:

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