Sorting an Array

About advanced scenes, and the Thyme scripting language used in Algodoo.

Sorting an Array

Postby phunHunger » Tue May 03, 2011 8:50 pm

Hello,

Is there a simple way to sort an array so that I can get the two highest values within that array?
Noobs are the best ;)
User avatar
phunHunger
 
Posts: 31
Joined: Mon Apr 26, 2010 10:01 pm

Re: Sorting an Array

Postby bozbez » Tue May 03, 2011 10:45 pm

Simple, no. Possible, yes; I can write you a function to do it if you want :)
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: Sorting an Array

Postby bozbez » Wed May 04, 2011 9:05 am

Right, here is da code:
Code: Select all
scene.my.arrayCounter = (array, arrayLength)=>{biggest = (-inf);secondbiggest=(-inf);for(arrayLength, (r)=>{array(r)>biggest?{biggest = array(r)}:{array(r)>secondbiggest?{secondbiggest = array(r)}:{}}}); numset = [biggest,secondbiggest]; numset}

Usage:
Code: Select all
samplearray = [1,2,3,4];
scene.my.arraycounter(samplearray,4)

It wil then return an array with the two biggest numbers, the biggest first. You must get the length of the array correct.

Note:
It leaves the original array alone.
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: Sorting an Array

Postby phunHunger » Wed May 04, 2011 9:27 pm

Cheers! I'll see if I can get this thing to work...
Noobs are the best ;)
User avatar
phunHunger
 
Posts: 31
Joined: Mon Apr 26, 2010 10:01 pm

Re: Sorting an Array

Postby phunHunger » Wed May 04, 2011 9:37 pm

Excellent, it works really well, thanks so much :)
Noobs are the best ;)
User avatar
phunHunger
 
Posts: 31
Joined: Mon Apr 26, 2010 10:01 pm

Re: Sorting an Array

Postby bozbez » Wed May 04, 2011 10:41 pm

I must be getting better at Thyme :)
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: Sorting an Array

Postby Someone Else » Thu May 05, 2011 5:29 pm

So... it sets biggest and secondbiggest to -inf, then methodically go through each value, setting biggest and secondbiggest where appropriate? That is, if current array value is greater than biggest, it sets biggest to that array value, then if not, checks if it is bigger than secondbiggest?
Then saves biggest and secondbiggest in another value?
Matthias Wandel is epic, in my humble opinion.
I love my brain...
TC42 wrote:Also, your sig is too big, please change it.

ARE YA HAPPY NOW?????

Thymechanic/Phundamentalist

Recently, I discovered something a lot of you probably already knew: Minecraft is awesome.
Due to this, I may not be as active as usual for a while.
User avatar
Someone Else
 
Posts: 1147
Joined: Sun Nov 21, 2010 10:53 pm
Location: The Milky Way Galaxy

Re: Sorting an Array

Postby bozbez » Thu May 05, 2011 5:31 pm

You got it :)
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests