2

Say i have following keys in hashtable:

"first1"
"second2"
"third3"
"forth4"

Is there a way to put key "third3" at the top. So that when i run func hash-table-keys i get following list:

("third3" "first1" "second2" "forth4")

I tried running remhash followed by puthash. But the order stays the same.

Sime1
  • 31
  • 2

1 Answers1

3

No, you cannot change the order of the keys in a hash table.

As @xuchunyang's comment points out, hash tables do not have an order in the way you're thinking about them. From the elisp manual node on hash tables:

The correspondences in a hash table are in no particular order. 

You may wish to order the keys in some form (say, for the purposes of accessing the values in some particular order), but that ordering will live outside the hash table itself. hash-table-keys returns a list of the table's keys, and you can manipulate that list just as any other list: sorting it, setfing it, and so on.

Dan
  • 32,584
  • 6
  • 98
  • 168