Sorting an array

Get help using Construct 2

Post » Mon Mar 05, 2012 12:58 pm

Added to the "Arrays" section.
Also @Ashley, I'd love for the array plugin to interface the js standard functions of arrays like pop, push, sort, etc...

That would make sorting and using arrays even easier and more powerful.Kyatric2012-03-05 12:58:53
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
304
S
123
G
98
Posts: 7,357
Reputation: 72,953

Post » Mon Mar 05, 2012 4:03 pm

It would be difficult to allow people to call array.sort without allowing them to provide a custom comparator function. The default sort in JS converts everything toString which can produce some bizarre results if you're not expecting it.
B
17
S
6
G
6
Posts: 113
Reputation: 4,161

Post » Mon Mar 28, 2016 3:44 pm

Just found this now, and as always R0J0 to the rescue. Thanks for the bubble sort tip!
B
21
S
6
Posts: 77
Reputation: 1,445

Post » Mon Apr 25, 2016 6:43 pm

Both are nice examples but it is kind of hard to with a 2D array.
I might not sure how to implement it based on the OP example.

thanks,
B
126
S
51
G
25
Posts: 401
Reputation: 22,867

Post » Mon Apr 25, 2016 7:56 pm

@jogosgratispro
For a 10x2 array for example, you'd have to swap around not only array.at(curX), but the other values too. But no matter, just use the sort action and don't worry about it.
B
94
S
33
G
128
Posts: 5,489
Reputation: 81,541

Post » Mon Jan 01, 2018 7:53 am

@Yann has a bug in his code: at the first loop iPos from 0 to Array.Width - 1 it's actually Array.Width - 2, otherwise it always replaces the max value with 0 and after more sorts the array has only 0's in it.
Black Bobby The Hole RPG game Greenlit with 303 votes.
B
44
S
10
G
1
Posts: 193
Reputation: 3,546

Post » Mon Jan 01, 2018 8:31 am

Je Fawk, you found a very old post. C2 now has "Array Sort" action which you can use to sort a 2D array.
B
17
S
9
G
125
Posts: 1,737
Reputation: 66,795

Post » Mon Jan 01, 2018 10:12 pm

@je-fawk yeah you're right, but at that time, in 2012, C2 didn't have bidirectional for loops, so the inner for loop was automagically skipped in the last iteration of the outer one, like in the wikipedia algorithm.
But because for loops are now bidirectional, since I don't know when, it does a for "i" from 10 to 9 instead of just skipping.
Good catch, I'll try to reupload a correction soon.
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

Post » Tue Jan 09, 2018 3:01 pm

@dop2000 i replied because i can't use/don't know how to use the sort they have implemented so i'm making my own :P

I got an array of 3 x ?? x 1, the height changes with the number of players and i'm trying to sort by score but yeah... their sort ... ???
[Number] [PlayerName] [Score]

So I'm stuck with making it myself :?

@Yann sorry, i didn't know there was a change in how loops worked before
Black Bobby The Hole RPG game Greenlit with 303 votes.
B
44
S
10
G
1
Posts: 193
Reputation: 3,546

Post » Tue Jan 09, 2018 3:08 pm

Hey @je-fawk!


Here's what the manual states about the Sort action:

Sort
Sorts the order of elements on an axis in ascending order. Note that in multidimensional arrays this sorts based on the first element on the axis. For example sorting the X axis in a 2D array will sort the order of the columns based on the elements at Y co-ordinate 0, while preserving the contents of each column.

So, if you want to sort by score, just change the order of columns in your array. Also, I'd suggest you make your array ?? x 3 x 1...

Hope this helps. Cheers!
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
22
S
6
G
3
Posts: 717
Reputation: 3,996

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests