So, I just completed the camper leaderboard project, scratching my head on how to get data sorted when clicking on the table header, only to realize that when JSON data is loaded, it is being loaded already sorted. Why is it so ? Why isn’t the data loaded following the order in which it was built ?
Could you post your code, preferably in a CodePen or some other online editor?
In the example project, the leaderboard is also loaded with data sorted by the number of recent points. The third camper on the JSON input is ArielLeslie with 217 points, whereas the 3rd camper that shows when you load the page is diomed with 264 points (3rd highest ranking).
I am only loading the data via a standard AJAX call. The AJAX call is in a function which I call in the componentDidMount function, quite like in the example project.
The API endpoints given in the assignment, https://fcctop100.herokuapp.com/api/fccusers/top/recent and https://fcctop100.herokuapp.com/api/fccusers/top/alltime, will return the top 100 users in their respective categories sorted in descending order. We can see the order of the raw JSON response for /alltime matches the output in the example project.
Ok, I got it, that explains things. Thanks mate! So, with this specific project we don’t actually need to figure out the sorting part, as it is not requested to enable toggling between ascending and descending orders.
However, if I understood correctly, generally speaking, with an AJAX call, the JSON data would be loaded following the same order as it is presented on the server.
JSON data is, regardless of where you get it, just data. Once you have it you can sort it any which way you please. Oftentimes you won’t receive the data in any order, and it may be different each time you make the request, even if you’re getting the same data.