Welcome Guest, Not a member yet? Register   Sign In
sorting some array
#1

[eluser]Fabdrol[/eluser]
Hey fokes!

I am facing a problem, probably not CI special but I do need some help.
I have an array like below, and need to sort the main array on the "start" keys of the sub array. But, I have no idea how!

Code:
$out = array(
  array('start' => '913156', 'title' => 'Lorum Ipsum'),
  array('start' => '913789', 'title' => 'Lorum Ipsum'),
  array('start' => '819156', 'title' => 'Lorum Ipsum'),
  array('start' => '768909', 'title' => 'Lorum Ipsum'),
  array('start' => '798900', 'title' => 'Lorum Ipsum')
);

so I need to sort the main $out array by the 'start' keys of the sub-arrays.

Thanks for your help!

Fabian
#2

[eluser]drewbee[/eluser]
Check out usort. You can specify your own sorting algorithm.
#3

[eluser]Fabdrol[/eluser]
I also found that function, but I don't get how I create such an sorting algoritm.. That's my problem!
#4

[eluser]Dam1an[/eluser]
I've never used usort (didn't know of it, althoiugh figured somethhing like it would exist) but maybe something like this would work

Code:
function my_sort($a, $b) {
    return $b['start'] - $a['start'];
}

$out = array(
    array('start' => '913156', 'title' => 'Lorum Ipsum'),
    array('start' => '913789', 'title' => 'Lorum Ipsum'),
    array('start' => '819156', 'title' => 'Lorum Ipsum'),
    array('start' => '768909', 'title' => 'Lorum Ipsum'),
    array('start' => '798900', 'title' => 'Lorum Ipsum')
);

// Print the original
print_r($out);

// Now sort it
usrot($out, 'my_sort');

echo '<br /><br />';

// print out the sorted array
print_r($out);

(I'm not sure if the order b-a is correct or if it should be a-b...)
#5

[eluser]drewbee[/eluser]
With this algorithm, you have to remember that you need to return -1 0 +1 for less than, equal, or greater than. (Edit: actually, you only need to return negative, 0 or positive -- txs dam1an)

Give the title's a different name so you can test correctly. This will be my first time using usort as well, but I think I have a handle on it Big Grin

You were close though Big Grin This sorts from lowest to highest. If you want to change the order, you do it the other way around, simply switch to return a -1 when a > b

Code:
&lt;?php

function my_sort($a, $b)
{
    if ($a['start'] == $b['start'])
    {
        return 0;
    }
    return ($a['start'] < $b['start']) ? -1 : 1;
}

$out = array(
    array('start' => '913156', 'title' => 'Title 1'),
    array('start' => '913789', 'title' => 'Title 2'),
    array('start' => '819156', 'title' => 'Title 3'),
    array('start' => '768909', 'title' => 'Title 4'),
    array('start' => '798900', 'title' => 'Title 5')
);
echo print_r($out);

echo '<br/><br/>';

usort($out, 'my_sort');

echo print_r($out);
?&gt;

Output...
Quote:Array ( [0] => Array ( [start] => 913156 [title] => Title 1 ) [1] => Array ( [start] => 913789 [title] => Title 2 ) [2] => Array ( [start] => 819156 [title] => Title 3 ) [3] => Array ( [start] => 768909 [title] => Title 4 ) [4] => Array ( [start] => 798900 [title] => Title 5 ) ) 1

Array ( [0] => Array ( [start] => 768909 [title] => Title 4 ) [1] => Array ( [start] => 798900 [title] => Title 5 ) [2] => Array ( [start] => 819156 [title] => Title 3 ) [3] => Array ( [start] => 913156 [title] => Title 1 ) [4] => Array ( [start] => 913789 [title] => Title 2 ) ) 1
#6

[eluser]Dam1an[/eluser]
Oh, I didn't know it had to be -1, 0 or 1, I assumed it was like the Java comparisons, which use negative, zero and positive which would have meant mine would have worked
#7

[eluser]drewbee[/eluser]
Well, perhaps I shouldn't speculate so much. It looks like yours works as well, Dam1an, except in desc order.
I seen the example on the site, and assumed. We all know what to think about assumers... heh heh heh.

So, both of our algorithms are the same. For yours to switch order, it would simply be changing the points of b - a to a - b.

Quote:Array ( [0] => Array ( [start] => 913789 [title] => Lorum Ipsum ) [1] => Array ( [start] => 913156 [title] => Lorum Ipsum ) [2] => Array ( [start] => 819156 [title] => Lorum Ipsum ) [3] => Array ( [start] => 798900 [title] => Lorum Ipsum ) [4] => Array ( [start] => 768909 [title] => Lorum Ipsum ) )
#8

[eluser]Dam1an[/eluser]
It's ok, I initially assumed as well, although had we actually read the description for the compare function...
Quote:The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

And I wanted descending order Tongue (ok, I was too lazy to think which way it should be, so just made a point you might need to change it)
#9

[eluser]drewbee[/eluser]
hahaha... reading the docs? Naw, we are too good for that! haha... I actually caught the first box of the input parameters but completely skipped the second part.
#10

[eluser]Dam1an[/eluser]
I normally skim/skip them as well, but was needed to prove myself Tongue

:roll: I mean I always RTFM Wink




Theme © iAndrew 2016 - Forum software by © MyBB