Sorting an Array
8 posts • Page 1 of 1
Sorting an Array
Hello,
Is there a simple way to sort an array so that I can get the two highest values within that array?
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 
-

phunHunger - Posts: 31
- Joined: Mon Apr 26, 2010 10:01 pm
Re: Sorting an Array
Simple, no. Possible, yes; I can write you a function to do it if you want 
-

bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: Sorting an Array
Right, here is da code:
Usage:
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.
- 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.
-

bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: Sorting an Array
I must be getting better at Thyme 
-

bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: Sorting an Array
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?
Then saves biggest and secondbiggest in another value?
Matthias Wandel is epic, in my humble opinion.
I love my brain...
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.
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.
-

Someone Else - Posts: 1147
- Joined: Sun Nov 21, 2010 10:53 pm
- Location: The Milky Way Galaxy
8 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest




