简体   繁体   中英

ImmutableJS - How to sort a date in descendent order

I have this function that sorts dates in ascendent order (from the first date to the last date), I'm using immutableJS https://facebook.github.io/immutable-js/docs/#/Map/sortBy :

export const OrderedMapSelector = (state) => {
  if ( state.allRetrospectivesMap ) {
    return state.allRetrospectivesMap.sortBy((retro) => retro.get('date'))
  } else {
    return Map({})
  }
}

I want to sort it by descendent order, from the last date to the first date. How can I do this?

Thanks :)

You can reverse() the results:

state.allRetrospectivesMap.sortBy((retro) => retro.get('date')).reverse()

Or you can use sort() with a comparator:

state.allRetrospectivesMap.sort((a, b) => b.get('date') - a.get('data')) // or any other relevant comparison between the dates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM