I need to order an array objects mapped out into a div by one of the objects values (int)
I am building a react app that will allow someone to filter out universities based on their majors (i.e. show GWU,ASU, UoA, etc. if the user chose computer engineering. In the returned data, the university model has an upvoteCount which is essentially equivalent to a "like". If the user chooses to view the returned data from highest to lowest upvote count or vice versa, how would I do this with the objects returned and placed into a div?
I have created a component that will create a card with the university's information on it. I thought I might be able to use index to identify the component and React.useState() to evaluate the user has selected view by highest to lowest. Then place a statement to see if these objects need to be sorted or not. I am hoping there is a way to do this through the div and not through the query.
{data.universityByMajor.map((university) => (
<div className="home-box ">
<UniversityList
key={university._id}
index={university.upvoteCount}
_id={university._id}
university_name={university.university_name}
university_img={university.university_image}
upvotes={university.upvoteCount}
/>
</div>
))}
This is what the data.universityByMajor looks like:
[{
"__typename":"University",
"_id":"65312d6d895868ce018b1891",
"university_name":"The George Washington University",
"university_image":"GWU_img.png",
"upvoteCount":2
},
{
"__typename":"University",
"_id":"65312d6d895868ce018b1895",
"university_name":"George Mason University",
"university_image":"GMU_img.jpg",
"upvoteCount":0
}]
Any help would be greatly appreciated
To order an array of objects mapped out into a div by one of the objects values (int), you can use the following steps:
Here is an example of how to do this in React: