Author Topic: How to sort an array?  (Read 5635 times)

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
How to sort an array?
« on: May 21, 2012, 07:43:05 PM »
Code: [Select]
myarray = { 3, 2, 5, 1, 4 }
How do I sort this into:

Code: [Select]
myarray = { 1, 2, 3, 4, 5 }
?



Is it...

Code: [Select]
table.sort(myarray)
?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: How to sort an array?
« Reply #1 on: May 22, 2012, 02:39:47 AM »
I've developed this code:


Code: [Select]
function Sort(ar, num)
-- sorts a 0-indexed array into order, and returns the sorted array
-- inputs are (array to be sorted, number of elements to sort)
-- typically inputs will be like: myarray = Sort(myarray, # myarray)

-- Lua is stupid and sorts tables from 1-to-arraylength instead of indexing from 0 like any sensible language
-- so we must copy the incoming array to a temporary renumbered array.
temp = {}
for i = 1,num+1 do
temp[i] = ar[i-1]
end
-- copied to temporary array
-- now sort
table.sort(temp)
-- sorted.  now copy back to real array
for i = 1,num+1 do
ar[i-1] = temp[i]
end
return ar
end

Also tonight I learned a useful feature of lua.  If you write something like:

x = # myarray

then x is now equal to the length of the array!

So for example, you can do:

Code: [Select]
for ID = 0, # myarray
myarray[ID] = nil
end

That would find all existing entries in the array and set them to nil.

# <array> returns the length of the array.

If you have elements [0 ], [1], and [2], using # array will return 2.  If you just have elements [1] and [2] then it will still return 2.  It seems to start counting from 1..
« Last Edit: May 22, 2012, 02:45:11 AM by annikk.exe »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: How to sort an array?
« Reply #2 on: May 22, 2012, 05:01:16 AM »
You can also use

Code: [Select]
for i in ipairs(tablename) do
  //DO AWSUMZ STUFF HEER
end

to iterate over every element in an array.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: How to sort an array?
« Reply #3 on: May 22, 2012, 08:06:31 AM »
How does that work?  What is ipairs?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: How to sort an array?
« Reply #4 on: May 22, 2012, 09:56:46 PM »
Annikk, for your 3D engine I suggest you use arrays to sort the verticles like Objects in OO programming.

It's easy to do:

Code: [Select]
Verticles= {}

for i = 1, 40 do

Verticles[i] = {x = 0, y = 0, z = 0} -- You can have many more variables, there shouldn't be any limit except for the Memory consumption...

end

With that you can do
Code: [Select]
Verticles[x].x = something instead of
Code: [Select]
VerticleX[x] = something :)
« Last Edit: May 22, 2012, 10:00:26 PM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: How to sort an array?
« Reply #5 on: May 22, 2012, 10:57:12 PM »
That's a cool technique Aino..  definitely a lot neater than vertex3dX and vertex3dY.  I might switch to that when I go back and tidy up/optimise the engine.

By the way, it's actually vertices rather than verticles.
It's pronounced like "Verr-Tiss-uz" if that makes sense... not "verr tick ulls"

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: How to sort an array?
« Reply #6 on: May 22, 2012, 11:25:18 PM »
Oh, thought it was Verticle, but mistakes are made :P

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: How to sort an array?
« Reply #7 on: May 23, 2012, 05:46:11 PM »
Verticle is a funny name though.. :>  It makes me laugh.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: How to sort an array?
« Reply #8 on: May 25, 2012, 01:31:46 AM »
Sorry I haven't got back to you about ipairs. Exams and stuff.

Have a link it'll explain better than I will.
http://lua-users.org/wiki/TablesTutorial