Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 表1.sort-throws“;无效的订单函数“;_Sorting_Lua - Fatal编程技术网

Sorting 表1.sort-throws“;无效的订单函数“;

Sorting 表1.sort-throws“;无效的订单函数“;,sorting,lua,Sorting,Lua,我正在开发一个简单的朋友系统,并想用一些 规则 我比较了两个朋友的状态、级别和离线时间 注:朋友有3种状态(在线=3,忙=2,离线=1) 这是我的密码 local function compare(friend1,friend2) local iScore1 = 0 local iScore2 = 0 if friend1["eStatus"] > friend2["eStatus"] then iScore1 = iScore1 + 1 e

我正在开发一个简单的朋友系统,并想用一些 规则

我比较了两个朋友的状态、级别和离线时间

注:朋友有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)
本地函数比较(friend1、friend2)
本地iScore1=0
本地iScore2=0
如果friend1[“eStatus”]>friend2[“eStatus”],则
iScore1=iScore1+1
结束
如果friend1[“iLevel”]>friend2[“iLevel”],则
iScore1=iScore1+1
结束
如果friend1[“iOfflineTime”]iScore2
结束
表.排序(FriendData,比较)
当我添加几个朋友时,它会工作。但当我获得更多朋友时,它会抛出异常“排序的顺序函数无效”。
有人能告诉我怎么修吗?:)

多亏了@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 --They are both 0.
            return  friend1["accountID"] > friend2["accountID"]
        end
        return iScore1 > iScore2
    end
    table.sort(FriendData,compare)
    
    本地函数比较(friend1、friend2)
    本地iScore1=0
    本地iScore2=0
    如果friend1[“eStatus”]>friend2[“eStatus”],则
    iScore1=iScore1+100
    如果朋友1[“eStatus”]friend2[“iLevel”],则
    iScore1=iScore1+10
    如果朋友1[“i水平”]<2[“i水平”]那么
    iScore2=iScore2+10
    结束
    如果friend1[“iOfflineTime”]friend2[“iOfflineTime”]则
    iScore2=iScore2+1
    结束
    如果iScore1==iScore2,那么它们都是0。
    返回friend1[“accountID”]>friend2[“accountID”]
    结束
    返回iScore1>iScore2
    结束
    表.排序(FriendData,比较)
    
    如果iScore1=iScore2会发生什么?如果这些值中的任何一个相同怎么办?如果iScore1==iScore2Check out这个一阶函数无效,我希望函数将返回false,因为
    compare(friend1,friend2)
    compare(friend2,friend1)
    return
    true
    。确实如此。Thx寻求帮助。:)