Shh
Shh

Reputation: 309

React Immutable JS How to order a map

I have a search action which returns the following in props:

rank: List(...)
   0: "3"
   1: "2"
   2: "1"
   3: "4"

results: Map(...)
   0: Array[2]
      0: "2"
      1: Record(...)
         0: Array[2]
            0: "firstName"
            1: "Bob"
         1: Array[2]
            0: "lastName"
            1: "Smith"
       ...

How can I order the Results map by the values in the Rank list? When I use results.map() it orders them by ID (1, 2, 3, 4).

Upvotes: 0

Views: 179

Answers (1)

Leeroy_Wonkledge
Leeroy_Wonkledge

Reputation: 351

You should avoid mixing immutable type and javascript type.

const Person = Record({
    lastname: null,
    firstname: null,
    id: null,
};

So, results is now a Map which contains Record of Person

result = Map(...)
    35: Record(...) { id: 35, lastname: 'smith', firstname: 'bob' }
    27: Record(...) { id: 27, lastname: 'smith', firstname: 'Clara' }
    9: Record(...) { id: 9, lastname: 'dupont', firstname: 'michael' }
    3: Record(...) { id: 3, lastname: 'taylor', firstname: 'morgan' }

 const ResultOrdered = rank.map( id => result.get(id));

So, ResultOrdered is a list of RecordPerson ordered by their rank

Upvotes: 2

Related Questions