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

0 Members and 1 Guest are viewing this topic.

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### How to sort an array?
« on: May 21, 2012, 11:43:05 AM »
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
• Posts: 1,794
##### Re: How to sort an array?
« Reply #1 on: May 21, 2012, 06:39:47 PM »
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 arend`
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] = nilend`
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 21, 2012, 06:45:11 PM by annikk.exe »

#### Pilchard123

• Tester
• Old Oak
• Thank You
• -Given: 0
• Posts: 899
• Eufloria: Yes
##### Re: How to sort an array?
« Reply #2 on: May 21, 2012, 09:01:16 PM »
You can also use

Code: [Select]
`for i in ipairs(tablename) do  //DO AWSUMZ STUFF HEERend`
to iterate over every element in an array.

#### annikk.exe

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

#### Aino

• Ent
• Thank You
• -Given: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: How to sort an array?
« Reply #4 on: May 22, 2012, 01: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 doVerticles[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, 02:00:26 PM by Aino »

#### annikk.exe

• Achiever
• Ent
• Thank You
• -Given: 0
• Posts: 1,794
##### Re: How to sort an array?
« Reply #5 on: May 22, 2012, 02: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: 1
• Posts: 1,483
• They'll eat you next!
• Eufloria: Yes
##### Re: How to sort an array?
« Reply #6 on: May 22, 2012, 03:25:18 PM »
Oh, thought it was Verticle, but mistakes are made

#### annikk.exe

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

• Tester
• Old Oak
• Thank You
• -Given: 0