list.sortList() is highly confusing
I'm attempting to make an online leaderboard, and the leaderboard is a list with objects that contain the user data like high score and username. The documentation for the list.sortList() function is skimpy at best, so I had no idea how to use it. I found a post on the forum, used the code from that, and it got me no farther. How am I supposed to sort my leaderboard in descending order by
money (see client code farther below)? This is the relevant code from the server here here:
serverUpdate = function() for connection in server.new_connections print(connection) end for message in server.messages print(message.data) insertthing = true for i2 = 0 to leaderboard.length - 1 if leaderboard[i2].username == message.data.username then insertthing = false end end if insertthing == true then leaderboard.insert(message.data) end storage.set("leaderboard", leaderboard) end leaderboard.sortList( function(a1,a2) return a2.money - a1.money end ) end
Every tick of the game, the client sends the
userdata object to the server; to be clear, I have tested everything and there are no problems with the data sending/receiving. The
userdata object that the client is sending appears like so:
userdata = object username = "" money = 0 end
The game sets the username and money variables separately.
In conclusion, as you (hopefully) noticed, the offending line here is in the server code,
leaderboard.sortList( function(a1,a2) return a2.money - a1.money end ).
So, is the method I'm using for sorting incorrect?