table.sort 抛出 "invalid order function"
2018-12-29 3:14:10
收藏:0
阅读:112
评论:1
我正在开发一个简单的好友系统,希望能按照一些规则对 friendData 进行排序。
我比较了两个好友的状态、等级和离线时间。
PS:一个好友有3个状态。(在线=3,忙碌=2,离线=1)
以下是我的代码。
local function compare(friend1,friend2)
local iScore1 = 0
local iScore2 = 0
if friend1["eStatus"] > friend2["eStatus"] then
iScore1 = iScore1 + 1
end
if friend1["iLevel"] > friend2["iLevel"] then
iScore1 = iScore1 + 1
end
if friend1["iOfflineTime"] < friend2["iOfflineTime"] then
iScore1 = iScore1 + 1
end
return iScore1 > iScore2
end
table.sort(FriendData,compare)
当我添加几个朋友时,它可以工作。但是当我有更多的朋友时,它抛出了异常 "invalid order function for sorting"。 有人能告诉我如何修复它吗? :)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

感谢 @Paul Hebert 和 @Egor Skriptunoff ,我终于搞明白了。
关键在于 compare(a,b) 和 compare(b,a) 应该返回不同的结果。
具体而言:
当 iScore1 == iScore2 时,应该用一个唯一的值进行比较(例如 accountID)。
不同的比较值应该有不同的分数。
下面是新代码。
local function compare(friend1,friend2) local iScore1 = 0 local iScore2 = 0 if friend1["eStatus"] > friend2["eStatus"] then iScore1 = iScore1 + 100 elseif friend1["eStatus"] < friend2["eStatus"] then iScore2 = iScore2 + 100 end if friend1["iLevel"] > friend2["iLevel"] then iScore1 = iScore1 + 10 elseif friend1["iLevel"] < friend2["iLevel"] then iScore2 = iScore2 + 10 end if friend1["iOfflineTime"] < friend2["iOfflineTime"] then iScore1 = iScore1 + 1 elseif friend1["iOfflineTime"] > friend2["iOfflineTime"] then iScore2 = iScore2 + 1 end if iScore1 == iScore2 then --它们都是0。 return friend1["accountID"] > friend2["accountID"] end return iScore1 > iScore2 end table.sort(FriendData,compare)