Sorting an array

0 favourites
• 23 posts
From the Asset Store
A master sorting Puzzle fun and addictive puzzle game! A challenging yet relaxing game to exercise your brain!
• I have a 2d Array (matrix) of 10x3. I want to order each row of that array, given the index column (which would be 0).

For example if I have:

3, name2, 4

1, name1, 2

2, name3, 1

I want it to be:

1, name1, 2

2, name3, 1

3, name2, 4

I was going to try a bubble sort, but I suppose there should be a better method (specially since javascript has a sort() function...)

• Wouldn't this do it?

``````myArray.sort(function (a, b)
{
return a[0] - b[0];
});
``````
• I can't use javascript inside construct and I suppose I can't submit to the arcade if I modify the javascript...

• How would you try bubble sort? loop / comparison? Well, if C2 doesn't have a built-in function to do this, depending on the size of your Array, DO NOT use bubble sort, it's the most slow method to sort.

• I know it's the slowest, but it's also the simplest and my array size is 10, so it wouldn't be unnoticeable.

I'm not going to try to implement quick sort for a 10 value array LOL.

As how I would implement it, I was going to use a temporary array, but it will be a mess and I want to avoid it ;__;

• Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Ah ok then ^^

I wish i've already used arrays, so i could offer some help here :X

• I'm sorting the highscore table, I suppose it will go like this:

``````for i=9 to 1:
if a[i][0] > a[i-1][0]:
temp[0] = a[i-1][0]
temp[1] = a[i-1][1]
temp[2] = a[i-1][2]
a[i-1][0] = a[i][0]
a[i-1][1] = a[i][1]
a[i-1][2] = a[i][2]
a[i][0] = temp[0]
a[i][1] = temp[1]
a[i][2] = temp[2]
``````

I'm going to try it now...

• Here is an implementation of bubble sort if it's of any help:

http://dl.dropbox.com/u/5426011/examples%209/bubble_sort.capx

• here's an implementation of the selection sort algo

selectionSort.capx

more details on wikipedia holder of the truth of the universe

http://en.wikipedia.org/wiki/Selection_sort

• Thanks R0J0hound & Yann!

Kyatric - could you add this to the sticky "How To" thread? Sorting is an issue quite often!

• 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.

• 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.

• Just found this now, and as always R0J0 to the rescue. Thanks for the bubble sort tip!

• 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,

• 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.

• 23 posts