1

I have an element of the sidebar, on the page of my application I want to sort them by the number of likes. How can I implement sorting at the stage of the map operation, so that the components are drawn correctly?

 return (
        <section>
            <Popular/>
            <div>
                {this.props.object.map((summary, i) =>
                    <SideBarPost id={summary.id}
                                 firstName={summary.User.firstName}
                                 lastName={summary.User.lastName}
                                 title={summary.title}
                                 createdAt={summary.createdAt}
                                 likes={summary.likes.length}
                                 key={i}
                    />)
                }
            </div>
         </section>
dance2die
  • 33,976
  • 36
  • 126
  • 188
Иван
  • 55
  • 7

1 Answers1

1

Hassan Imam's solution in the comments helped:

You can use something like this this.props.object.sort((a,b) => b.likes.length - a.likes.length).map(/*component logic here*/);

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
Иван
  • 55
  • 7