sort a table, please help
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
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

sort a table, please help

Started by blackwings, 19 July, 2005, 21:34:50

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

blackwings

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 :(


scsigirl

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

scsigirl

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

blackwings

QuoteOriginally 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.


blackwings

#4
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 =
local Array={"A","B","C"}

Becomes this =
local table={
	["A"]=1,
	["B"]=1,
	["C"]=1,
}

My latest try, didn't work either :( =
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


Mogli

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.

blackwings

#6
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


VidFamne

Just a wild idea ;)
Havent tested it;
local tbl = {}
local array = {"A", "B", "C", "D"}
	for i = 1, table.getn(array) do
		tbl[array[i]] = 1
	end
return tbl

VidFamne

#8
>>HERE<<
is a link, that could give some hints, about sort.

Mogli

#9
dunno blackwings., but this is the exsample posted
in LUA USERS ORG just a bit simpler,
maybe it is what you want, dunno

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:

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.

blackwings

maybe I misunderstood, but with this code =
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) =
[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


Mogli

#11
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 :)

blackwings

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:
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


SMF spam blocked by CleanTalk