Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Comp function in table.sort

edited December 2012 in Questions Posts: 58

I don't get how the comp function works as the second parameter of table.sort. I want to call my own custom function instead of the standard function, but I don't know how to set up this function. Could someone explain what exactly the comp function does, and how I could make my own function to use in place of the original one?

Comments

  • Jmv38Jmv38 Mod
    edited December 2012 Posts: 3,295

    Hello. Here is an example i wrote to sort t , a table of vec3

        local s = {}
        local i
        for i=1,#t do
            s[i] = vec4(t[i].x,t[i].y,t[i].z,i)
        end
        local function compVec3(a,b)
            local out = false
            if floor(a[1])<floor(b[1]) then out = true
            elseif floor(a[1])==floor(b[1]) then
                if floor(a[2])<floor(b[2]) then out = true
                elseif floor(a[2])==floor(b[2]) then
                    if floor(a[3])<floor(b[3]) then out = true end
                end
            end
            return out
        end
        table.sort(s,compVec3)
    

    the last line applies the the sort to table s.
    the comVec3 returns true when a is strictly below b.
    If a=b it returns false.
    That's all about 'comp'.
    Because i want to keep what is the original position in of each vector in table t, i have added a 4rth column to s that contains the index i. After the sort i scan the table s to get the indexes i which are now in the good order . Is that clear now?

  • table.sort sorts a table using the quicksort algorithm (which is not important here).

    It iterates over the table, at each step taking two elements from it and comparing them. The comparison function tells the sorting algorithm if the two elements are already in order (true) or not (false).

    This is an equivalent of the standard comparison rule:


    t = {1, 8, 5, 4} # Return true if a < b function lesserthan(a, b) return a < b end table.sort(t, lesserthan) print(table.concat(t, ", ")) >> 1, 4, 5, 8

    The other way round:


    t = {1, 8, 5, 4} # Return true if a > b function greaterthan(a, b) return a > b end table.sort(t, greaterthan) print(table.concat(t, ", ")) >> 8, 5, 4, 1

    Now invent a predicate (means comparison function) yourself.


    t = {1, 8, 5, 4} # Return true if a is odder than b. # This is only possible if a is odd and b is even. function odderthan(a, b) odd_a = math.mod(a, 2) odd_b = math.mod(b, 2) return odd_a > odd_b end table.sort(t, odderthan) print(table.concat(t, ", ")) >> 1, 5, 8, 4

    You will have the "oddest" values listed first, then the even values. Note that no statement was made about the order of the numbers within their repective part, just even or odd.

Sign In or Register to comment.