Discord
Login
Community
DARK THEME

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?

Your code for sorting looks ok but try this
leaderboard.sortList( function(a,b) return b.money < a.money end )
I think that will solve it but I'm not entirely sure

Hmm. Apparently the function is supposed to return a difference, so it has to be subtraction instead of inequality. What a nightmare this is...

Does sortList produce any output at all? even incorrect output is ok now
Do you store usernames in list as well (multidimensional list)?
Try printing out stuff tied to function that's the best way of figuring out whats wrong
because code for sorting is right and i checked docs original func leaderboard.sortList( function(a1,a2) return a2.money - a1.money end ) was correct so it must be error in parsing data from list or something like that.

From the docs: (Fundamentals -> Microscript Programming -> Sorting a List)

The compareFunction you provide has to accept two arguments (which we will call a and b) and should return:

  • a negative number - when a must be sorted before b (a is less than b)
  • zero - when a and b have an equal position regarding to the desired ordering criterion
  • a positive number - when a must be sorted after b (a is greater than b)

The sortList function of the List class can be called without any parameter - and it will also sort .

I don't know if it's a bug or a feature.

  list = [ 'c', 'b', 'c', 'a', 'z', '1', '9', 'x', 'a', '0', '1', 'b', 'z', 'a']
  list.sortList()
  print( list )

@Loginus I think it's a feature. It probably uses a default sorting function like this:

function(a, b) 
  return a < b and -1 or 1
end

Which works on strings as well. You can sort names alphabetically like that, for example.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community