Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ sorting a table

sorting a table

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Renny   Canada  (26 posts)  Bio
Date Sat 25 Apr 2015 07:42 AM (UTC)
Message
I want to sort a table based on two comparison operations.


t = { 
[1] = { fruit = pear, color = red, rank = 0 }, 
[2] = { fruit = apple, color = orange, rank = 1 }, 
[3] = { fruit = orange, color = green, rank = 2 }, 
[4] = { fruit = potatoe, color = black, rank = 1 },
}


I want to first sort the table by rank numerically, and then sort it by color alphabetically.

So the order would be,


1 = orange, green, 2
2 = potatoe, black, 1
3 = apple, orange, 1
4 = pear, red, 0


I am aware of this function,



table.sort(t, function(a, b) return a.rank > b.rank end)



but don't know how to do a more complicated sort.
Top

Posted by Fiendish   USA  (2,551 posts)  Bio   Global Moderator
Date Reply #1 on Sat 25 Apr 2015 01:24 PM (UTC)
Message
Maybe
table.sort(t, function(a, b) if a.rank == b.rank then return a.color > b.color else return a.rank > b.rank end)

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Renny   Canada  (26 posts)  Bio
Date Reply #2 on Sat 25 Apr 2015 04:14 PM (UTC)
Message
Fiendish said:

Maybe
table.sort(t, function(a, b) if a.rank == b.rank then return a.color > b.color else return a.rank > b.rank end)



gives me a syntax error

unexpected symbol near ')'
Top

Posted by Fiendish   USA  (2,551 posts)  Bio   Global Moderator
Date Reply #3 on Sat 25 Apr 2015 06:16 PM (UTC)

Amended on Sat 25 Apr 2015 06:19 PM (UTC) by Fiendish

Message
Fixing the syntax error left as a learning exercise for the reader. :) If you uncombine the comparison function from the sort invocation, as in

function mysort(a,b)
...

table.sort(t, mysort)

The fix should become obvious.

Also in your table you need to put the colors and fruits in quotation marks, brcause they are strings.

Also potato doesn't end with an e and isn't a fruit.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Renny   Canada  (26 posts)  Bio
Date Reply #4 on Sat 25 Apr 2015 08:33 PM (UTC)
Message
Fiendish said:

Also potato doesn't end with an e and isn't a fruit.


Thx this works perfect
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,187 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.