[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Another table.sort thread
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Another table.sort thread

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


Posted by Trevize   (21 posts)  [Biography] bio
Date Wed 22 Jun 2005 07:10 PM (UTC)
Message
Ok, I know this questions has already been answerd in: "http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=5514";, and that table.sort is explained here: "http://www.lua.org/manual/5.0/manual.html#5.4"

But I still dont get it, maybe Im an complete idiot or something but how does table.sort work?

This is how my current test.lua file looks like, im trying to figure out how table.sort is working but no mather what I do, I dont get it to work.

afflictions = {
	{ name = "addiction", priority = 8 },
	{ name = "agoraphobia", priority = 7 },
	{ name = "asthma", priority = 6 },
	{ name = "claustrophobia", priority = 9 },
	{ name = "clumsiness", priority = 6 },
	{ name = "confused", priority = 10 },
}

function test1()
	for k, v in afflictions do
		Note("Name: " .. tostring(v.name) .. ". Priority: " .. tostring(v.priority))
	end
end

function test2()
		table.sort (afflictions)
end

The "afflictions" table is "alot" bigger in the real file.

This is my current scripting error message when trying to call function test2:
attempt to compare two table values
stack traceback:
[C]: in function `sort'
[string "Script file"]:20: in function `test2'
[string "Command line"]:1: in main chunk

The main file is alot bigger and more complicated but since I cant event get this one to work I dont see the reason to bring the whole thing up here.

One more thing, this is proberly quite important. In my main file I want to sort a table like:
table1 = {
{ name = "addiction"},
{ name = "agoraphobia"},
}
after the priority stated in table2 where name in table1 is equal to the name in table2.
table2 = {
{ name = "addiction", priority = "8", syntax = "eat nightshade" },
{ name = "agoraphobia", priority = "7", syntax = "eat orphine" },
}
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Wed 22 Jun 2005 08:53 PM (UTC)
Message
Quote:
table.sort (table [, comp])

Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the size of the table (see 5.4). If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.


Pay attention to the part about 'comp'. What Lua is telling you is that it has no way of sorting tables, which makes sense, right, because what does { ... } < { ... } resolve to?

So, you have to give it a function that takes two tables, table1 and table2, and returns true if table1 is considered "less than" (i.e., comes before in the sorting order) than table2.

Back to your case, I don't think there's much use in sorting the first table according to the values in the second. You may as well just sort the second.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sun 30 Oct 2005 12:27 AM (UTC)
Message
Also see this post:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6036

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


5,207 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]