E
E
Egor2017-08-21 18:20:18
JavaScript
Egor, 2017-08-21 18:20:18

How to sort after applying normalizr?

There is an api request on the react/redux stack. The following data comes in:

[
  {...},
  {...},
  {...},
]

An array with objects, inside the objects there are more fields with arrays of objects. With the help of normalizr, I do normalization and get resultobjects entities. Fields from entitiesare stored in store. Then the task is to sort the object by some field, but normalizr turns the array into an object and it becomes difficult to sort it. The question is how to do it right?
My options:
1. Do sorting before applying normalizr. The downside is that then the store will have to write data that is not normalized, but what came from the server, or somewhere at the stage after the data arrives from the server, pull the store, take from there by which field you need to sort, sort and normalize it after that .
2. After normalization, create another array, which will contain objects with id and fields by which you can sort. Sorting on it, then iterate over it and substitute id. For example: entities.users[id]. The downside is that normalizr already creates an array to iterate over id result. You will either have to change it or make a new one.
If anyone has experience with this or ideas please let me know.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Gamega, 2017-08-21
@DarkSir

somehow it's all far-fetched, who's stopping you from writing something like

result.sort((a, b) => entities.data[a].sortField - entities.data[b].sortField).map(id=>this.renderData( entities.data[id]) )

V
Vitaly, 2017-08-21
@vshvydky

Lodash unionby

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question