|
| 20 Sep 2015 05:16 PM |
I have never used table.sort and never understood it, how can I sort a table from high to low value of a property of the table I need the highest rap to be at the top the table looks would like this
Stuff = { Item = {Product = {Rap = 194,Name = "a name of something",morestuff = "blah"}} Item = {Product = {Rap = 4234,Name = "a name of somethingelse",morestuff = "blahaa"}} Item = {Product = {Rap = 14254,Name = "a name of something",morestuff = "blaaffh"}} }
|
|
|
| Report Abuse |
|
|
|
| 20 Sep 2015 05:17 PM |
| I forgot the ", between the tables" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:18 PM |
The keys replace each other so what you're doing with that Stuff table is a waste of time, in the end you'll only have 1 pair, not 3.
And table.sort only sorts arrays, dictionary-part of the array has no 'order' so you can't sort it. |
|
|
| Report Abuse |
|
|
|
| 20 Sep 2015 05:19 PM |
table.sort(stuff,function(a,b) return a.Rap > b.Rap end) |
|
|
| Report Abuse |
|
|
|
| 20 Sep 2015 05:20 PM |
| Wait so the above thing wouldn't work?? :( |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 20 Sep 2015 05:24 PM |
| @cnt How DOES the second argument to table.sort work?? If it returns true then a > b if not then b>a ?? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:25 PM |
| If it returns true, it puts a in front of b, otherwise it puts b in front of a. |
|
|
| Report Abuse |
|
|
| |
|
|
| 20 Sep 2015 05:28 PM |
Oh cool! Also what's the problem with string indexes?? Since tab[1] and tab[str] are pretty similar??? tab = {"a","b"} tab = {str = a,str2 = b} |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:28 PM |
| Yes, Lua is a fantastic language. People who don't like Lua are generally bad at it and don't understand how anything works. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:29 PM |
| Because as I said before, dictionaries don't have any order. They way hashmaps are generally implemented makes it so you can't actually order them. |
|
|
| Report Abuse |
|
|
|
| 20 Sep 2015 05:44 PM |
table.sort(Stuff,function(a,b) return a.Product.Rap > b.Product.Rap end)
that works for me? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:45 PM |
| You think it works, but it's not. |
|
|
| Report Abuse |
|
|
|
| 20 Sep 2015 05:47 PM |
| so the fact I looped through it printing rap in order its still is not sorted? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Sep 2015 05:50 PM |
It's not, stop lying. The most obvious reason being keys are unique, when you create your table there is only 1 pair inside, not 3. |
|
|
| Report Abuse |
|
|