Author Topic: Help with sorting a table  (Read 3554 times)

0 Members and 1 Guest are viewing this topic.

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Help with sorting a table
« on: 02 January, 2009, 18:56:02 »
hey guys

here is something i have done till now,

i have a table
Code: [Select]
["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...

PtokaX forum

Help with sorting a table
« on: 02 January, 2009, 18:56:02 »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 725
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Help with sorting a table
« Reply #1 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.:

Code: [Select]
[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.

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Re: Help with sorting a table
« Reply #2 on: 03 January, 2009, 07:09:24 »
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.:

Code: [Select]
[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
Code: [Select]
temp={}
for v in pairs(table)
temp[table[v]["pts"]]=v
end

using the above code on the table show below
Code: [Select]
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
Code: [Select]
[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

Code: [Select]
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...

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Re: Help with sorting a table
« Reply #3 on: 03 January, 2009, 09:14:40 »
I mean kindly explain wit short examples. Pls :)

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

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 725
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Help with sorting a table
« Reply #4 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 American Idiot has already stated). table.insert, table.sort & friends will only work on arrays. Examples of arrays:
Code: [Select]
arr = {"joe", "bob", "sam"}
-- this is equivalent to:
{[1]="joe", [2] = "bob, [3] ="sam"}
However, this is an array too:
Code: [Select]
{[1]="joe", [15] = "bob, [38665] ="sam"}Arrays by definiton have only numerical indices. Now to your problem. This code:
Code: [Select]
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:
Code: [Select]
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.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 725
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: Help with sorting a table
« Reply #5 on: 03 January, 2009, 18:03:06 »
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.

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Re: Help with sorting a table
« Reply #6 on: 04 January, 2009, 16:09:57 »
OK, I see that you need explanation on the difference between tables and arrays. Arrays are tables with numerical indices (as American Idiot has already stated). table.insert, table.sort & friends will only work on arrays. Examples of arrays:
Code: [Select]
arr = {"joe", "bob", "sam"}
-- this is equivalent to:
{[1]="joe", [2] = "bob, [3] ="sam"}
However, this is an array too:
Code: [Select]
{[1]="joe", [15] = "bob, [38665] ="sam"}Arrays by definiton have only numerical indices. Now to your problem. This code:
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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

Code: [Select]
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
Code: [Select]
====================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...

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Re: Help with sorting a table
« Reply #7 on: 05 January, 2009, 15:15:46 »
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...

Offline satya

  • Fulll Member
  • ****
  • Posts: 64
  • Karma: +3/-5
Re: Help with sorting a table
« Reply #8 on: 05 January, 2009, 16:44:57 »
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...

PtokaX forum

Re: Help with sorting a table
« Reply #8 on: 05 January, 2009, 16:44:57 »