Skip to content

Allow sorting on hashes #9853

Open
Open
@koenbollen

Description

Is your feature request related to a problem? Please describe:
I would like to be able to sort a list of hashes, which is currently not possible because a hash is userdata and is not comparable.

A workaround I now use is:

local player_list = { hash("p1234"), hash("p2345") }
table.sort(player_list, function(a, b)
  return hash_to_hex(a) < hash_to_hex(b)
end)

Describe the solution you'd like:
I would love if a hashes are comparable (e.g. hash1 < hash2) so table.sort just works like this:

local player_list = { hash("p1234"), hash("p2345") }
table.sort(player_list)

Describe alternatives you've considered:
Currently I use the hash_to_hex builtin to make them strings and compare these. It works, but it's a very inefficient way of sorting a list of uints..
Sorting them before hashing is also not possible in my usecase.

Additional context:
This is important for us because of multiplayer synchronization. In this example I keep track of a list of player uids, and want them sorted for leader election (the first in the list is leader if the leader leaves).
In my p2p game I also often use player0 < player1 to determine which player will be polite in case of a race condition.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    engineIssues related to the Defold enginefeature requestA suggestion for a new featureluaIssues related to the use of Lua

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions