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
  • 35,807
  • 39
  • 131
  • 194
Иван
  • 55
  • 7

2 Answers2

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
  • 50,140
  • 28
  • 121
  • 140
Иван
  • 55
  • 7
0

const filter1 = [...users].sort((a,b)=> a.name.localeCompare(b.name)) setUsers(filter1)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 15:57
  • can you please explain your code ? – Ahmed Sbai Jan 31 '23 at 20:56