From the course: Rendering Data in React
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Solution: Sort the list of objects alphabetically - React.js Tutorial
From the course: Rendering Data in React
Solution: Sort the list of objects alphabetically
- [Instructor] All right. On the left, you can see a list and we have sorting options. I can already sort by rating, (laughs) actually, already sorted by rating, by price, but not yet by title. When I click Sort by title, you can see it's not really sorted by title. So, let's go ahead and implement this. In order to implement it, we need to expand the logic of how we get the sorted books. So currently, we're sorting by price, and if it's not sorted by price, we sort it by rating. So, let's go ahead and add an extra if option. So, I'm going to say if the sortType is set to title, then go ahead and return. And then, I'm just going to compare the title of the first book to the second book. And now, if I go ahead and select title, you can see it's been sorted by title. If you go to price, it's sorted by price. And if you go back to title, it's then again sorted by title, so it seems to be working. Let's go ahead and verify our code by testing it. And as you can see, the test results are…