Sorting an array

Get help using Construct 2

Post » Sun Mar 04, 2012 7:22 pm

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...)kirby2012-03-04 19:22:18
B
22
S
3
G
6
Posts: 59
Reputation: 4,450

Post » Sun Mar 04, 2012 7:35 pm

Wouldn't this do it?
[CODE]
myArray.sort(function (a, b)
    {
        return a[0] - b[0];
    });
[/CODE]
B
17
S
6
G
6
Posts: 113
Reputation: 4,161

Post » Sun Mar 04, 2012 7:47 pm

I can't use javascript inside construct and I suppose I can't submit to the arcade if I modify the javascript...
B
22
S
3
G
6
Posts: 59
Reputation: 4,450

Post » Sun Mar 04, 2012 8:04 pm

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.
English is not my native language. Sorry for any mistakes and feel free to correct me if needed.
B
14
S
5
G
3
Posts: 110
Reputation: 3,225

Post » Sun Mar 04, 2012 8:16 pm

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 ;__;kirby2012-03-04 20:16:26
B
22
S
3
G
6
Posts: 59
Reputation: 4,450

Post » Sun Mar 04, 2012 8:41 pm

Ah ok then ^^

I wish i've already used arrays, so i could offer some help here :X
English is not my native language. Sorry for any mistakes and feel free to correct me if needed.
B
14
S
5
G
3
Posts: 110
Reputation: 3,225

Post » Sun Mar 04, 2012 8:48 pm

I'm sorting the highscore table, I suppose it will go like this:
[code]
for i=9 to 1:
if a[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[0]
    a[i-1][1] = a[1]
    a[i-1][2] = a[2]
    a[0] = temp[0]
    a[1] = temp[1]
    a[2] = temp[2]
[/code]
I'm going to try it now...
B
22
S
3
G
6
Posts: 59
Reputation: 4,450

Post » Sun Mar 04, 2012 9:13 pm

Here is an implementation of bubble sort if it's of any help:
http://dl.dropbox.com/u/5426011/examples%209/bubble_sort.capx
B
79
S
24
G
54
Posts: 4,752
Reputation: 40,767

Post » Sun Mar 04, 2012 9:18 pm

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
Last edited by Yann on Sun Sep 14, 2014 9:25 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Mar 05, 2012 12:49 pm

Thanks R0J0hound & Yann!

@kyatric - could you add this to the sticky "How To" thread? Sorting is an issue quite often!
B
42
S
19
G
12
Posts: 723
Reputation: 13,911

Next

Return to How do I....?

Who is online

Users browsing this forum: linkman2004, SpartCrafter and 27 guests