sorting array

Mar 16, 2010 at 3:56 PM

HI,

I have found your code and it's a very good job.

I have a question. Is it possible to sort an array of values without to use object ?

 

 

$values = array(
'0' => array('nom' => 'Mellet', 'prénom' => 'Jean-Luc'  , 'age => 49'),
'1' => array('nom' => 'Gobert', 'prénom' => 'Christophe', 'age => 30'),
'2' => array('nom' => 'Masse' , 'prénom' => 'Martin'    , 'age => 35')
);

$values = array(

'0' => array('name' => 'John', 'age => 49'),

'1' => array('name' => 'Peter', , 'age => 30'),

'2' => array('name' => 'Graham' ,   , 'age => 35')

);

Is it possible to sort this array first by by name and then by age ?

Thanks ,

Jean-Luc !

 

Coordinator
Mar 16, 2010 at 6:15 PM

Try "orderBy('$x => $x->name')->thenBy('$x => $x->age')" 

Mar 16, 2010 at 9:21 PM

I have tried this:

 

$values = array(
 '0' => array('name' => 'John'  , 'age' => 49),
 '1' => array('name' => 'Peter' , 'age' => 30),
 '2' => array('name' => 'Peter' , 'age' => 25),
 '3' => array('name' => 'Graham', 'age' => 35)
);
$result = from('$value')->in($values)
->orderBy('$value => $value->age')
->thenBy('$value => $value->name')
->select('$value');
var_dump($result);

$values = array(

 '0' => array('name' => 'John'  , 'age' => 49),

 '1' => array('name' => 'Peter' , 'age' => 30),

 '2' => array('name' => 'Peter' , 'age' => 25),

 '3' => array('name' => 'Graham', 'age' => 35)

);

$result = from('$value')->in($values)

->orderBy('$value => $value->age')

->thenBy('$value => $value->name')

->select('$value');

var_dump($result);

I get the array order by reverse key:
array(4) {
  [0]=>
  string(6) "Graham"
  [1]=>
  string(5) "Peter"
  [2]=>
  string(5) "Peter"
  [3]=>
  string(4) "John"
}