我正在开发一个简单的好友系统,并希望通过一些规则对好友数据进行排序。
我比较了两个好友的状态、等级和离线时间。
PS:一个好友有三种状态(在线=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)
当我添加几个朋友时,它可以正常工作。但是当我的朋友数量增加时,它会抛出异常“无效的排序函数顺序”。 有人能告诉我如何修复它吗?:)
compare(friend1, friend2)
和compare(friend2, friend1)
都有可能返回true
。 - Egor Skriptunoff