Author Topic: sort a table, please help  (Read 4394 times)

0 Members and 1 Guest are viewing this topic.

Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
sort a table, please help
« on: 19 July, 2005, 21:34:50 »
Can anyone tell me how to sort a table. I have tried using table.sort and table.setn, but no succes to make it work :(


PtokaX forum

sort a table, please help
« on: 19 July, 2005, 21:34:50 »

Offline scsigirl

  • Junior Member
  • **
  • Posts: 11
  • Karma: +0/-0
Bubble sort for tables
« Reply #1 on: 20 July, 2005, 02:46:02 »
or this short routine will do the trick too...

-- QUICK BUBBLE SORT FOR TABLES

function SortTable(Table)
   Idx = 1
   while Table[Idx] do
       if Idx > 1 and Table[Idx] < Table[Idx-1] then
      Temp = Table[Idx]      -- save current entry
      Table[Idx] = Table[Idx-1]   -- move prior down 1
      Table[Idx-1] = Temp      -- save current up 1
      Idx = Idx-1         -- recheck prior entry
       else
      Idx = Idx+1         -- check next entry
       end
   end
   return
end

Offline scsigirl

  • Junior Member
  • **
  • Posts: 11
  • Karma: +0/-0
(No subject)
« Reply #2 on: 20 July, 2005, 02:59:16 »
and if you want it to be case insensitive then use this instead

if Idx > 1 and strlower(Table[Idx]) < strlower(Table[Idx-1]) then

Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
(No subject)
« Reply #3 on: 20 July, 2005, 19:27:46 »
Quote
Originally posted by Mutor
...elaborate sort algorythms here.


http://lua-users.org/wiki/OrderedTable
tried them, but still, its all random, just like my own attempts on doing it. All I want is that it add the values from from a array to a table, in the same order that they were in the array.


Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
(No subject)
« Reply #4 on: 21 July, 2005, 18:55:14 »
I know, but I wanted the values from array to be put as key in table, the same order as they are in array.

Like this =
Code: [Select]
local Array={"A","B","C"}
Becomes this =
Code: [Select]
local table={
["A"]=1,
["B"]=1,
["C"]=1,
}

My latest try, didn't work either :( =
Code: [Select]
local Array={"A","B","C","D","E","F","G","H","I","J"}
table = {}
sorted = {}
for key, value in array do
table[key]=value
count2 = 1
for key2,value2 in table do
sorted[value2]=count2
if sorted[count2] == (table[key]==value) then
count2=count2+1
end
end
end
end
« Last Edit: 21 July, 2005, 19:12:03 by blackwings »


Offline Mogli

  • Fulll Member
  • ****
  • Posts: 59
  • Karma: +0/-0
(No subject)
« Reply #5 on: 21 July, 2005, 21:58:11 »
hey blackwings Mutor is right,
There is no way to sort the keys of a table the
way you want it..  you can only iterate over it
in a sorted manner, well that function actually
only creates a new table with the keys as
index, sorts it and then iterates over that table.

And scsigirl, nice way of sorting, but
the LUA mechanism is the fastest for really unsorted tables,
it uses binary function, so your iteration would be faster
if the table was sorted where as the LUA sort
calcs the insertposition new, although if the next
entrie would fit to the wanted sort.
So depends actually how the given table looks like.

Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
(No subject)
« Reply #6 on: 21 July, 2005, 23:54:44 »
your own example show the problem (its not in order) :( =
   Index: A  <--->  Value:1
   Index: C  <--->  Value:1
   Index: B  <--->  Value:1
   Index: E  <--->  Value:1
   Index: D  <--->  Value:1
   Index: G  <--->  Value:1
   Index: F  <--->  Value:1
   Index: I  <--->  Value:1
   Index: H  <--->  Value:1
   Index: J  <--->  Value:1

its random, well not random, the first one comes correct, then every two swiches places
« Last Edit: 21 July, 2005, 23:55:19 by blackwings »


Offline VidFamne

  • Triple Ace
  • **
  • Posts: 173
  • Karma: +2/-0
(No subject)
« Reply #7 on: 22 July, 2005, 00:25:11 »
Just a wild idea ;)
Havent tested it;
Code: [Select]
local tbl = {}
local array = {"A", "B", "C", "D"}
for i = 1, table.getn(array) do
tbl[array[i]] = 1
end
return tbl

Offline VidFamne

  • Triple Ace
  • **
  • Posts: 173
  • Karma: +2/-0
(No subject)
« Reply #8 on: 22 July, 2005, 18:22:44 »
>>HERE<<
is a link, that could give some hints, about sort.
« Last Edit: 22 July, 2005, 18:24:58 by VidFamne »

Offline Mogli

  • Fulll Member
  • ****
  • Posts: 59
  • Karma: +0/-0
(No subject)
« Reply #9 on: 23 July, 2005, 14:59:06 »
dunno blackwings., but this is the exsample posted
in LUA USERS ORG just a bit simpler,
maybe it is what you want, dunno

Code: [Select]
t = { }
t.A = 1
t.B = 2
t.C = 3
t.D = 4
t.E = 5
t.F = 6
t.G = 7
t.H = 8

print( "Normal iteration" )
-- will iterate over the table bei next index
table.foreach( t, print )
-- will not work since we have no numerical indexes
table.foreachi( t, print )
 -- so we need to create a sorted array
-- as in the Thread from Lua User Org
ts = {}
table.foreach( t, function( i, _ ) table.insert( ts, i ) end )
table.sort( ts )
-- now iterate over the sorted array but display the value from the inassociative table
 print( "Sorted iteration" )
table.foreachi( ts, function( i, v )
print( v, t[v] )
end )

Results:

Code: [Select]
Normal iteration
A       1
C       3
B       2
E       5
D       4
G       7
F       6
H       8
Sorted iteration
A       1
B       2
C       3
D       4
E       5
F       6
G       7
H       8

By the way there is no way to sort the indexes of an Inassociative table, meaning a table with no numerical indexes, cause these will always be changed.
« Last Edit: 23 July, 2005, 15:04:33 by Mogli »

Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
(No subject)
« Reply #10 on: 23 July, 2005, 21:39:45 »
maybe I misunderstood, but with this code =
Code: [Select]
bot = "test"
t={"A","B","C","D","E","F","G","H","I","J"}

ts = {}

function Main()
SendToAll( "Normal iteration" )
table.foreach(t,function(i,v) table.insert( ts, v ) end )
table.sort(ts)
table.foreachi( ts, function( j, v )SendToAll( v, t[v] )end )
end

function ChatArrival(user,data)
for k,w in ts do
SendToAll(bot, "k: "..k.." - w: "..w)
end
end
I get this result(when using the chatarrival) =
Code: [Select]
[21 31 16] k: 1 - w: A
[21 31 16] k: 2 - w: B
[21 31 16] k: 3 - w: C
[21 31 16] k: 4 - w: D
[21 31 16] k: 5 - w: E
[21 31 16] k: 6 - w: F
[21 31 16] k: 7 - w: G
[21 31 16] k: 8 - w: H
[21 31 16] k: 9 - w: I
[21 31 16] k: 10 - w: J
it is tv sure, but if you see


Offline Mogli

  • Fulll Member
  • ****
  • Posts: 59
  • Karma: +0/-0
(No subject)
« Reply #11 on: 24 July, 2005, 00:14:43 »
hmm and now I don't understand it myself,
I thaught you have a table like
t["A"] = 1
t["B"] = 2

and not
t = { "A", "B" , ... }

but I know that if the index of a table is not a number then the order will be mor random like when traversing all fields.

Quote
-- QUICK BUBBLE SORT FOR TABLES

function SortTable(Table)
Idx = 1
while Table[Idx] do
if Idx > 1 and Table[Idx] < Table[Idx-1] then
Temp = Table[Idx] -- save current entry
Table[Idx] = Table[Idx-1] -- move prior down 1
Table[Idx-1] = Temp -- save current up 1
Idx = Idx-1 -- recheck prior entry
else
Idx = Idx+1 -- check next entry
end
end
return
end

back to that sort, I think it is the fastest actually, cause with binary, one would then still have to reindex all other variables, so thank you very much scsigirl for that nice cool code :)
« Last Edit: 24 July, 2005, 00:15:36 by Mogli »

Offline blackwings

  • Scripter
  • King
  • ******
  • Posts: 986
  • Karma: +12/-8
(No subject)
« Reply #12 on: 24 July, 2005, 20:14:05 »
some how, lua really wants the exact order of the uncomment array in a table(use the different arrays and see the result,
also in chatarrival use different tables to see different results). It must like sorting it after the binary data or something :baby:
Code: [Select]
Array={"A","B","C","D","E","F","G","H","I","J"}
--Array={"A","C","B","E","D","G","F","I","H","J"}

bot = "sorting"

Sorted = {}
done1 = {}
done2 = {}

function Main()
f=1
tAdds = 2
tAddsY = 2
for i,v in Array do
Sorted[f]=v
if tAdds == 2 then
f = f+2
tAdds = 1
--Sorted[f]="X"
f = f+1
elseif tAdds == 1 then
f = f-1
tAdds = 2
end
end
g=1
for k,val in Sorted do
done1[g]=val
if tAddsY == 2 then
g = g+2
tAddsY = 1
--Sorted[f]="X"
g = g+1
elseif tAddsY == 1 then
g = g-1
tAddsY = 2
end
end
for ki,vol in done1 do
done2[vol]=ki
end
end

function ChatArrival(user,data)
for key,value in done2 do --change between the different tables to see the result
Reply2 = "Index: "..key.."  <--->  Value:"..value
SendToAll(bot, Reply2)
end
end


PtokaX forum

(No subject)
« Reply #12 on: 24 July, 2005, 20:14:05 »