0

EDIT

solved ! it was my fault, the mysql query and the php method, both are working perfectly. my very bad example cunfused myself. aktually they should be sorting products by favorite ( true / false ) and then by date.


i'm trying to sort a multidimensional array by two "cols" but it just won't work for me :O

I've serched and copyed all possible ways ( mysql - order by, php - array_multisort() ) but it still won't work for me....

here is the method i've tryed first:

// MySQL Query
SELECT * FROM `test` ORDER BY `name` ASC, `age` DESC;
// or
SELECT * FROM `test` ORDER BY `name`, `age`;
// or
SELECT * FROM `test` ORDER BY name, age;
// or
SELECT * FROM `test` ORDER BY name, age DECT;

// `name` is VARCHAR and `age` is INT

so , i've tryed all possible syntax, but the query wont give me the wanted result.

the original table :

ID    NAME     AGE
---------------------
1     adam     23
2     bernd    30
3     cris     22
4     dora     21
5     anton    18
6     brad     36
7     sam      41
8     ali      13

what i want to get from MySQP Query :

ID    NAME     AGE
---------------------  // first sort by name...
8     ali      13      // then sort by age
5     anton    18      // ...
1     adam     23      // for each "similar" name or names with "a", "b", ...
2     bernd    30      // and so on...
6     brad     36
3     cris     22
4     dora     21
7     sam      41

but it actually gives my either sorted by NAME ( ORDER BY name, age ) or AGE ( ORDER BY age, name;

as i get frustrated, i've decidet to just get that query and sort it in PHP...

well, i've also tryed some codes but they all don't work either.

here is the one i aktually did now:

// this array is just exported from mysql to debug without mysql query...
$test = array(
  array('id' => '1','name' => 'adam','age' => '23'),
  array('id' => '2','name' => 'bernd','age' => '30'),
  array('id' => '3','name' => 'cris','age' => '22'),
  array('id' => '4','name' => 'dora','age' => '21'),
  array('id' => '5','name' => 'anton','age' => '18'),
  array('id' => '6','name' => 'brad','age' => '36'),
  array('id' => '7','name' => 'sam','age' => '50'),
  array('id' => '8','name' => 'ali','age' => '13')
);

// ...print out the original array
foreach( $test as $key => $value ) {
    echo $value['name'] . " - " . $value['age'] . "<br>";
}

// here is the part where i am sorting...
$sort = array();
foreach ($test as $key => $value ) {
    $sort['name'][$key] = $value['name'];
    $sort['age'][$key] = $value['age'];
}

array_multisort($sort['age'], SORT_ASC, $sort['name'], SORT_DESC, $test);
// ...till here.

// reorder array and print the new sorted array
$sorted = array();
for( $i = 0; $i < count( $sort['name'] ); $i++ ) {
    array_push( $sorted, array( $sort['name'][$i], $sort['age'][$i] ) );
    echo $sort['name'][$i] . " - " . $sort['age'][$i] . "<br>";
}

but as you'll see if you test this, the sorting will just affect the AGE...

i dont know what i am doing wrong, prease tell me guys T_T

3
  • Why have you got "Adam" after "Anton"? If you're sorting by name, "Adam" comes before "Ali" comes before "Anton". Nov 28, 2012 at 12:51
  • @JeremySmyth yeah men you are right. i've got a really bad example here... sry for that , but it's solved already, my fault ^^
    – Ace
    Nov 28, 2012 at 12:55
  • If it's solved, well done! please mark whichever of the answers helped you, or add an answer (or edit your question) to better help future visitors to this question. Nov 28, 2012 at 12:58

2 Answers 2

2

It appears to me that you only want to sort on the first char of the name:

You should try:

SELECT * FROM `test` ORDER BY SUBSTR(name,0,1), age;

What you encountered is absolutely expected behaviour. Why? If you sort first by name, you get results sorted for the full strings in the name column, where

'adam' < 'ali' < 'anton'

If there were more than one rows with name='adam', but different ages, like this:

ID    NAME     AGE
---------------------
1     adam     23
5     anton    18
8     ali      13
9     adam     52

You would get this result for SELECT * FROM test ORDER BY name, age;

ID    NAME     AGE
---------------------
1     adam     23
9     adam     52
8     ali      13
5     anton    18

As it is first sorted by name column, where the two adam values are the same, and then sorts based on the age, where 23 is smaller than 52...

But now, sorting on first character of name, and age SELECT * FROM test ORDER BY SUBSTR(name,0,1), age;:

ID    NAME     AGE
---------------------
8     ali      13
5     anton    18
1     adam     23
9     adam     52
1
  • thats what i was looking for. but yes now i see it , it was my very bad example that confused me, now i see that the mySQL query AND the php method works well with that ^^ i'm sorry and thank you anyway ^^
    – Ace
    Nov 28, 2012 at 12:57
0

If I unserstand you correctly, what you want to do is to sort just on the first letter of the name, then on the age. - What you have gotten from mysql is just what I would expect from your query and data set. If you choose several columns for sort, the secound kicks in only if you have equal values in the first one, eg if you had two Adams, one 18 and one 20 years old, the two would be sorted by age. and if you had Adam and Beth, both 18, Beth would come after Adam in the "sort by age,name"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.