Help with sorting a table
 

News:

11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Help with sorting a table

Started by satya, 02 January, 2009, 18:56:02

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

satya

hey guys

here is something i have done till now,

i have a table
["pharoh"] = { --username
		["tm"] = "22:03:58", --lasttime wen he was in the hub
		["SeW"] = 0, -- something
		["pt"] = 10, --his pts in hub
		["SeM"] = 0, --sometjhing
		["dt"] = "Thursday the 01 of January ", --date wen he was in the hub
		["rdt"] = 0.00049875311720698, -- date wen he added pts
		["ip"] = "xx.xx.xx.xx",-- his ip
	},


in this table i store records of user with there nicks. now i want to sort all data in this table wrt to "pts" value of every user, in descending order. watt i want to do is wen ever ne user logs in i wanna show the pts table in descending order. how can i do dat(sorting of table's contents)?
sorry if i m sounding stupid  :)
Thanks satya

CG if u rem me i have came so far from where i started all thanks to u. Pls try to help me again.  :) :) :)

Eyes says everything,
Its on you how u read em...

bastya_elvtars

This is a very good question and I am happy to see some coding discussion here as it is quite scant nowadays. What I usually do is to create an array of "pt" values with the names belonging to them, e. g.:

[25] = {"joe", "sam", "bob"},
[30] = { "tom"}


I do this via metatables with a __newindex metamethod. You can find more on this in the Wiki. Should you have problems, feel free to ask.
Everything could have been anything else and it would have just as much meaning.

satya

Quote from: bastya_elvtars on 02 January, 2009, 20:13:04
This is a very good question and I am happy to see some coding discussion here as it is quite scant nowadays. What I usually do is to create an array of "pt" values with the names belonging to them, e. g.:

[25] = {"joe", "sam", "bob"},
[30] = { "tom"}


I do this via metatables with a __newindex metamethod. You can find more on this in the Wiki. Should you have problems, feel free to ask.

u know wat buddy dats wat i was thinking abt
bt i m having some prob, here i will xplain with xample

if there are more than one user has pt 25 as u showd they will be group, bt i don understand how wud dey get grouped
here is how i will do to make a temp table
temp={}
for v in pairs(table)
temp[table[v]["pts"]]=v
end


using the above code on the table show below
table={
	["pharoh1"] = { --username
		["tm"] = "22:03:58", --lasttime wen he was in the hub
		["SeW"] = 0, -- something
		["pt"] = 10, --his pts in hub
		["SeM"] = 0, --sometjhing
		["dt"] = "Thursday the 01 of January ", --date wen he was in the hub
		["rdt"] = 0.00049875311720698, -- date wen he added pts
		["ip"] = "xx.xx.xx.xx",-- his ip
	},
	["pharoh2"] = { --username
		["tm"] = "22:03:58", --lasttime wen he was in the hub
		["SeW"] = 0, -- something
		["pt"] = 10, --his pts in hub
		["SeM"] = 0, --sometjhing
		["dt"] = "Thursday the 01 of January ", --date wen he was in the hub
		["rdt"] = 0.00049875311720698, -- date wen he added pts
		["ip"] = "xx.xx.xx.xx",-- his ip
	},
	["pharoh3"] = { --username
		["tm"] = "22:03:58", --lasttime wen he was in the hub
		["SeW"] = 0, -- something
		["pt"] = 10, --his pts in hub
		["SeM"] = 0, --sometjhing
		["dt"] = "Thursday the 01 of January ", --date wen he was in the hub
		["rdt"] = 0.00049875311720698, -- date wen he added pts
		["ip"] = "xx.xx.xx.xx",-- his ip
	},
}


You see all of them have same pts so instead of gettin grouped they will get overwritten so i wanna know how to group them together.

and one more.  ;D

i have command in the hub where user can specify his new added shares, i want to log these shares i know how to do that
[username]={
	[1/1/08]={abc,xyz},
	[2/1/08]={abc,xyz},
	[3/1/08]={abc,xyz},
}

above is an example of dat table its logs the share accordin to his nick and date when he shared, now wat i want is if today he shares a say "YES MAN" now here i want to add the stuffs at the end of the table. i.e. the new share he does on that date gets appended to that date shares. Now while writing this i went through the LUA MANUAL and found this

table.insert (table, [pos,] value)

Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table (see ?2.5.5), so that a call table.insert(t,x) inserts x at the end of table t.


Is this how i have to do, kindly xplain with short explains.  ;D
sorry if i sound stupid,

thanks Satya

Eyes says everything,
Its on you how u read em...

satya

I mean kindly explain wit short examples. Pls :)

Eyes says everything,
Its on you how u read em...

bastya_elvtars

OK, I see that you need explanation on the difference between tables and arrays. Arrays are tables with numerical indices (as Mutor has already stated). table.insert, table.sort & friends will only work on arrays. Examples of arrays:
arr = {"joe", "bob", "sam"}
-- this is equivalent to:
{[1]="joe", [2] = "bob, [3] ="sam"}

However, this is an array too:
{[1]="joe", [15] = "bob, [38665] ="sam"}

Arrays by definiton have only numerical indices. Now to your problem. This code:
temp={}
  for v in pairs(table)
  temp[table[v]["pts"]]=v
end

they will sure get overwritten. If you want a structure like I posted above you need to do this:
temp={}
  for v in pairs(table)
  local WeNeed = table[v]["pts"] -- so we won't in struct Lua to do the same lookup many times
  temp[WeNeed] = {} or WeNeed -- checking for existence of this key/value pair in the table, if it does not exist, create
  table.insert(temp[WeNeed], v) -- the key is the "pts" number, the value is an array (= "list") of users
end

thus you will get the structure I ahve posted above.

Hope this is clear.
Everything could have been anything else and it would have just as much meaning.

bastya_elvtars

BTW if you don't mind (and even if you do :P) I have split the topic and moved to the appropriate forum.
Everything could have been anything else and it would have just as much meaning.

satya

Quote from: bastya_elvtars on 03 January, 2009, 14:06:35
OK, I see that you need explanation on the difference between tables and arrays. Arrays are tables with numerical indices (as Mutor has already stated). table.insert, table.sort & friends will only work on arrays. Examples of arrays:
arr = {"joe", "bob", "sam"}
-- this is equivalent to:
{[1]="joe", [2] = "bob, [3] ="sam"}

However, this is an array too:
{[1]="joe", [15] = "bob, [38665] ="sam"}

Arrays by definiton have only numerical indices. Now to your problem. This code:
temp={}
  for v in pairs(table)
  temp[table[v]["pts"]]=v
end

they will sure get overwritten. If you want a structure like I posted above you need to do this:
temp={}
  for v in pairs(table)
  local WeNeed = table[v]["pts"] -- so we won't in struct Lua to do the same lookup many times
  temp[WeNeed] = {} or WeNeed -- checking for existence of this key/value pair in the table, if it does not exist, create
  table.insert(temp[WeNeed], v) -- the key is the "pts" number, the value is an array (= "list") of users
end

thus you will get the structure I ahve posted above.

Hope this is clear.

As u said i have done the same, here take a look

local tmp={}
local dum
RepChart = function()
	for i in pairs(usrec) do
		dum=usrec[i]["pt"]
		if tmp[dum]==nil then
			tmp[dum]={}
		end
		table.insert(tmp[dum],i)
	end
	table.sort(tmp)
end


and wen i do this

RegConnected = function(user)
	local mSG
	mSG="\n\t\t\t"..string.rep("=",20).."Reputation toppers"..string.rep("=",20).."\n\n\t\t\t\tPoints\t\t\tUser\n"
	for i in pairs(tmp) do
		mSG=mSG.."\n\n\t\t\t\t"..i.."\t\t\t"
		for j in pairs(tmp[i]) do
			mSG=mSG..tmp[i][j]..", "
		end
	end
	Core.SendToUser(user,mSG)
end


here is wat i get
====================Reputation toppers====================

				Points			User


				25			achilese, 

				50			pappz, abhiu, 

				85			fellopian_dude, 

				15			coolraj, 

				30			coolhunk, blackjack, 

				45			--vampire--,


They are not sorted as i wanted :(, i even tried to use the function table.sort(tmp) bt it didn helped n i don even know how to use dat function. Can u pls lemme know where i m going wrong?????  ::) ::) ??? ???

Thanks Satya

Eyes says everything,
Its on you how u read em...

satya

Hey guys i am still waitin for reply. Pls mods i am waitin for a help

Eyes says everything,
Its on you how u read em...

satya

I have found a work around to get what i wanted. anyways thanks for time

Eyes says everything,
Its on you how u read em...

SMF spam blocked by CleanTalk