Jump to content




[SOLVED, Solution in post!] Sorting tables on alphabetical order


  • You cannot reply to this topic
5 replies to this topic

#1 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 15 June 2014 - 12:12 PM

Hello everyone!

My question is so simple that the most of you ask why I am asking this, but I am trying to alphabetically sort a table so it is easier searching for something. I am trying to sort the first index on alphabetical order. This is an example of what the table looks like:
data[aspect][1] = 64 --#aspect is a string, the second number indicates how many jars are there and the value is how much of an aspect is in a jar

I tried table.sort, but it is not working. Anyone have ideas?

Edited by GamerNebulae, 16 June 2014 - 10:33 AM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 June 2014 - 12:19 PM

EDIT: take a look at this link to also see the code that Bomb Bloke was talking about.

Edited by theoriginalbit, 15 June 2014 - 12:42 PM.
pardon me, making simple mistakes, I must be in need of sleep


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 June 2014 - 12:25 PM

You can't really sort the table as it is anyway - keys in the form of strings aren't intended to be retrieved in any sort of order. The strings themselves end up defining the order, and not in a manner that's easily predictable.

I'd take all the key names and shove them in a numerically indexed table, then table.sort() that. Funnily enough, that's what the script linked from here was crashing on (if only because Dire's code was getting unexpected results from a certain peripheral call).

#4 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 16 June 2014 - 10:32 AM

Thanks for the good responses everybody! It made me think of how I need to sort the table and I came up with a solution that is quite obvious, but still genius. This is a snippet of what I made on codepad:

Spoiler


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 16 June 2014 - 10:39 AM

By the by, you could cut down that initial table declaration somewhat:

data ={["Aer"] = {33,12,63}, ["Herba"] = {44,42,33}, ["Fabrico"] = {21,12,22}, ["Aqua"] = {22,44,55}}


#6 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 16 June 2014 - 11:04 AM

View PostBomb Bloke, on 16 June 2014 - 10:39 AM, said:

-snip-

The making of that table is automated, so it doesn't matter.

Edited by GamerNebulae, 16 June 2014 - 11:17 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users