1. i want to sort a particular table and i don't exactly know how to do it. my table has that rough makeup :

    table={

    [name1]={key1=value1, key2=value2, key3=value3},

    [name2]={key1=value4, key2=value5, key3=value6},

    [name3]={key1=value7, key2=value8, key3=value9},

    ...

    }

    where the "names" are not fixed, representing players in a raid group (the number of names are also not fixed). i'd like to sort the names in the table by key1, key2 or key3. i've re-checked the guide's way of doing it, but it assumes the table is an array, which is not my case. So i'm searching for any hint of what should the function that i would put in table.sort(table.function) should look like.

    i've also considered making it an array and putting the name as an actual key, but that would more or less destroy the simple way i had found to quickly search and access it, so i want to avoid it.

    i want to sort a particular table and i don't exactly know how to do it. my table has that rough makeup :

    table={

    [name1]={key1=value1, key2=value2, key3=value3},

    [name2]={key1=value4, key2=value5, key3=value6},

    [name3]={key1=value7, key2=value8, key3=value9},

    ...

    }

    where the "names" are not fixed, representing players in a raid group (the number of names are also not fixed). i'd like to sort the names in the table by key1, key2 or key3. i've re-checked the guide's way of doing it, but it assumes the table is an array, which is not my case. So i'm searching for any hint of what should the function that i would put in table.sort(table.function) should look like.

    i've also considered making it an array and putting the name as an actual key, but that would more or less destroy the simple way i had found to quickly search and access it, so i want to avoid it.

    This is covered on page 74.

    yes, i know that it is covered. as i said, i re-checked the way the guides do it, but it doesn't mention what to do when your table is NOT an array. in the book example, the guild table is an array of other tables because there is no key specified to each "member's" table right? this is not my case. i understand that the sorting function takes on 2 arguments, but can "a" and "b" be something other than specified_table[n] and specified_table[n+1] where "n" is a boring integer?

    when using pairs and ipairs. as i understood it, ipairs goes trough the table in an order specified by the array numbers and doesn'T count the non-array part of a table while pairs check each entry whatever they are and in the order they were input in the table. do i have to assume that table.sort only consider an ipairs-like way of thinking? or am i just not looking hard enough and my code is wrong?

    that :

    function test.sortbytier(a,b)

          return a.key1 < b.key1

    end 

    doesn't do anyting because it consider a and b to be array numbers. how to tell it that i want it to be name1 and name2 instead?

    yes, i know that it is covered. as i said, i re-checked the way the guides do it, but it doesn't mention what to do when your table is NOT an array. in the book example, the guild table is an array of other tables because there is no key specified to each "member's" table right? this is not my case. i understand that the sorting function takes on 2 arguments, but can "a" and "b" be something other than specified_table[n] and specified_table[n+1] where "n" is a boring integer?

    No, you can ONLY sort arrays. This following is a very simple pattern:

    local guild = {

    cladhaire = {class = "Rogue", level = 80},

    draoi = {class = "Druid", level = 70},

    mallaithe = {class = "Warlock", level = 45},

    }

    local sortTbl = {}

    for k,v in pairs(guild) do

    table.insert(sortTbl, k)

    end

    table.sort(sortTbl, function(a, b)

    return guild[a].class < guild[b].class

    end)

    for k,v in ipairs(sortTbl) do

    print(k, v, guild[v].class)

    end 

    when using pairs and ipairs. as i understood it, ipairs goes trough the table in an order specified by the array numbers and doesn'T count the non-array part of a table while pairs check each entry whatever they are and in the order they were input in the table. do i have to assume that table.sort only consider an ipairs-like way of thinking? or am i just not looking hard enough and my code is wrong?

     

    alright, i'm starting to see the light. so we basically have to use a temporary sort table to do the job. i understand that. your script runs fine, but when i try to put the names in guild in the order the sortTbl has them, it kinda fucks up. i figured i had to use another temporary table sortedguild to store all the data of guild with the proper order, then scrap guild and make it equal to sortedguild.

    function test.sortbytier()

       local sortTbl={}

       for k,v in  pairs(guild) do

          table.insert(sorttbl, k)

       end

      

       table.sort(sortTbl, function(a,b) return guild[a].Class < guild[b].Class

       end)

      

       for k,v in ipairs(sortTbl) do --doesn'T work anymore =_O

          print(k, v)

       end

      

       local sortedguild={} --backup sorted table

       for k,v in ipairs(sortTbl) do

          sortedguild[v]=guild[v]

       end

      

       for k,v in pairs(guild) do --scrap the guild table

          guild[k]=nil

       end

       guild=sortedguild

    end

    first off, when i add that piece of code after your own pattern, it suddenly refuses to print anything. and well the method i'm using to backup the data is obviously wrong because whenever i try accessing something in the new guild it gives me a nil value.

     

    I honestly have no idea what you're trying to do now.  There should be no "replacing" the old table and you'll never be able to have a non-array that's sorted in the way you want.  This code out of context is incredibly confusing.  Can you please tell me what you are trying to accomplish and why?  Don't give me code, explain to me in words what sort of data you have and what you are trying to accomplish with it.  Then I might be able to help.

    lol alright. i want to build a database named MemberTable that basically builds itself off your current raid group. every player entry is done exactly as i pictured : the name as key and a table as value, which contains info about the class, raidID, supgroup number and something called Tier which is defined by the user.

    i think it is fairly simple to go through that table and display everyone's information on a sort of list with columns for each info. what i wanna do is let the user sort the list by name, group, raidID or Tier. the problem is, as you kindly shown me, that i couldn'T sort the MemberTable because it wasn't an array. so i thought "ok so i need a second temporary table which will be an array and which CAN be sorted, then i can use the sorted values to rebuild the MemberTable in that order. i thought indexing the tables would be smoother, but i guess i was wrong. why is the MemberTable's entries' order so important? because it will be shown in that order on my frame. i designed it so that it would have to go through the MemberTable only once since it is an operation repeated fairly often. maybe my way of thinking is naive?

    i am beginning to consider having a simple ["name"]=playername entry in each member's personnal table, and redesign the whole MemberTable as an array...but that would mess up everything else.

    i think it is fairly simple to go through that table and display everyone's information on a sort of list with columns for each info. what i wanna do is let the user sort the list by name, group, raidID or Tier. the problem is, as you kindly shown me, that i couldn'T sort the MemberTable because it wasn't an array. so i thought "ok so i need a second temporary table which will be an array and which CAN be sorted, then i can use the sorted values to rebuild the MemberTable in that order. i thought indexing the tables would be smoother, but i guess i was wrong. why is the MemberTable's entries' order so important? because it will be shown in that order on my frame. i designed it so that it would have to go through the MemberTable only once since it is an operation repeated fairly often. maybe my way of thinking is naive?

    You should not be rebuilding the memberTable, it's order does not matter and never will.  It is a hash table, and cannot be sorted, and should not be sorted.  You can do two things:

    Change it so that membersTable is both an array and a hash table.  For example:

    local Cladhaire = {name = "Cladhaire", class = "Rogue", level = 70};

    memberTable["Cladhaire"] = Cladhaire

    table.insert(memberTable, Cladhaire)

    Now I can sort memberTable and ipairs() over it to display them in order, and I can still look up Cladhaire's information without having to loop over the entire table.   Alternatively you can do just what I proposed... ALWAYS have it as a hash table, and then sort/ipairs every time you need to display it. I'm not sure why that's a problem.. it's standard.

    well, as soon as i posted my above post, i thought i was very dump and this morning i changed my code to accomodate a simple array. its isn't as complicated as i thought i would be and it will be a lot less of a headache to do what i intend to do with it. i'll think about the hash/array mixed table. that could be useful too. 

    btw this is my first addon. i don't know much about standards ^^" (and even if i did, i tend to do things my own way by that is another issue). thank you for your help anyway. tables aren't as nebulous as they were (thought they still are a bit).

    Glad you were able to make it work!  Thanks for reporting back :P