Associatively sorting a table by value in Lua

前端 未结 6 1757
星月不相逢
星月不相逢 2020-11-28 11:47

I have a key => value table I\'d like to sort in Lua. The keys are all integers, but aren\'t consecutive (and have meaning). Lua\'s only sort function appears to be table.

6条回答
  •  一向
    一向 (楼主)
    2020-11-28 12:23

    I did a brief bit of Lua coding a couple of years ago but I'm no longer fluent in it.

    When faced with a similar problem, I copied my array to another array with keys and values reversed, then used sort on the new array.

    I wasn't aware of a possibility to sort the array using the method Kornel Kisielewicz recommends.

提交回复
热议问题