Search code examples
reactjsreduximmutabilityseamless-immutable

how to sort Immutable data


I am using 'seamless-immutable' with redux. But i have come across a situation where I cant sort my data.

//reducer

export const INITIAL_STATE = Immutable({
  data: [],
});

export const updateData = (state = INITIAL_STATE, action) =>
  Immutable(state.set('data', action.data));


const ACTION_HANDLERS = {
  [UPDATE_DATA]: updateData,
};
export default createReducer(INITIAL_STATE, ACTION_HANDLERS);

I am getting the data but when i try to sort eg: (say)arr.sort, it gives me: The sort method cannot be invoked on an Immutable data structure.

How can i sort the data? Thanks for the help.


Solution

  • I don't see your sort function being called but in order to do so you need to use the following:

    const sortedData = Immutable.asMutable(updateData).sort(f => f.dataFieldToSort);