0

My array looks like this

Array
(
    [0] => Array
        (
            [last_name] => Kournikova
            [first_name] => Anna
            [gender] => Female
            [date_of_birth] => 6/3/1975
            [favorite_color] => Red
        )

    [1] => Array
        (
            [last_name] =>  Hingis
            [first_name] => Martina
            [gender] => Female
            [date_of_birth] => 4/2/1979
            [favorite_color] => Green
        )

    [2] => Array
        (
            [last_name] =>  Seles
            [first_name] => Monica
            [gender] => Female
            [date_of_birth] => 12/2/1973
            [favorite_color] => Black
        )

    [3] => Array
        (
            [last_name] => Abercrombie
            [first_name] =>  Neil
            [gender] =>  Male
            [date_of_birth] =>  2/13/1943
            [favorite_color] =>  Tan
        )

    [4] => Array
        (
            [last_name] => Bishop
            [first_name] =>  Timothy
            [gender] =>  Male
            [date_of_birth] =>  4/23/1967
            [favorite_color] =>  Yellow
        )

    [5] => Array
        (
            [last_name] =>  Kelly
            [first_name] =>  Sue
            [gender] =>  Female
            [date_of_birth] =>  7/12/1959
            [favorite_color] =>  Pink
        )

    [6] => Array
        (
            [last_name] => Smith
            [first_name] => Steve
            [gender] => Male
            [date_of_birth] => 3/3/1985
            [favorite_color] => Red
        )

    [7] => Array
        (
            [last_name] => Bonk
            [first_name] => Radek
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Green
        )

    [8] => Array
        (
            [last_name] => Bouillon
            [first_name] => Francis
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Blue
        )

)

The outcome is this

Kournikova Anna Female 6/3/1975 Red
Hingis Martina Female 4/2/1979 Green
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Kelly Sue Female 7/12/1959 Pink
Smith Steve Male 3/3/1985 Red
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue

I want the outcome to be Females before Males by last_name ascending

Hingis Martina Female 4/2/1979 Green
Kelly Sue Female 7/12/1959 Pink
Kournikova Anna Female 6/3/1975 Red
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue
Smith Steve Male 3/3/1985 Red

How can I apply usort() to sort them by that order?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael Stokes
  • 401
  • 1
  • 10
  • 24
  • 3
    Possible duplicate of [PHP: How to sort by two values then a field by ASC in an array using usort()?](http://stackoverflow.com/questions/38037042/php-how-to-sort-by-two-values-then-a-field-by-asc-in-an-array-using-usort) – u_mulder Jun 26 '16 at 11:31
  • Hope following link resolve your issue http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – Abhishek Jun 26 '16 at 11:33
  • @u_mulder I had to ask a new question just to make my issue a lot clearer. – Michael Stokes Jun 26 '16 at 11:34
  • 1
    Asking two questions is not the way to do that; edit the first one to clarify. – AD7six Jun 26 '16 at 11:40
  • I asked him to do that, perhaps I should have asked to rewrite the original question. Sorry. – KIKO Software Jun 26 '16 at 11:42
  • Don't use usort for this, it's not very fast. Let me show you a better way. – The Onin Apr 26 '17 at 10:26
  • Yo Michael, me and others invested time writing answers, and not paying any attention to them - that's not cool. – The Onin Apr 28 '17 at 07:38
  • Hey Nino, thank you for the answer you provided me. I don't understand what you mean I haven't paid any attention to them since I already had the solution worked out LAST YEAR. Let me tell you what's not cool, you bashing me here in this community like I don't give a damn. What exactly do you want from me? This question was from LAST YEAR and I had everything worked out already! – Michael Stokes Apr 29 '17 at 12:55

2 Answers2

0

Sort by females befores males, and then by last_name ASC:

(assuming your array is contained in $data)

array_multisort(
    array_column($data, 'gender'), SORT_ASC,
    array_column($data, 'last_name'), SORT_ASC,
    $data
);

array_multisort is much faster than usort - on a 100k array dataset, about 30x faster.

The Onin
  • 5,068
  • 2
  • 38
  • 55
-1

Here's one solution.

function compare($array1,$array2)
{
  // if gender is equal we need to look at the last name
  if ($array1['gender'] == $array2['gender'])
  {
    // so we look at the last name
    return strcasecmp($array1['last_name'],$array2['last_name']);
  }
  // this happens to work for gender, F is before M
  return strcasecmp($array1['gender'],$array2['gender']);
}

$data = [['last_name'      => 'Bonk',
          'first_name'     => 'Radek',
          'gender'         => 'Male',
          'date_of_birth'  => '6/3/1975',
          'favorite_color' => 'Green'],

         ['last_name'      => 'Seles',
          'first_name'     => 'Monica',
          'gender'         => 'Female',
          'date_of_birth'  => '12/2/1973',
          'favorite_color' => 'Black'],

         ['last_name'      => 'Abercrombie',
          'first_name'     => 'Neil',
          'gender'         => 'Male',
          'date_of_birth'  => '2/13/1943',
          'favorite_color' => 'Tan']];

usort($data,'compare');

foreach ($data as $key => $value)
{
  echo "<pre>$key: ".print_r($value,TRUE)."\n</pre>";
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33