Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array of objects, I would like to sort. I would like to sort the array on two values for each object:

E.g

Array (
 [0] => "TeamName time (Id) - CoachName",
 [1] => "TeamName time (Id) - CoachName"
...
)

I would like first sort on the CoachName and then on time such that

Before sort

Array(
[0] => "FirstTeam 12:00-12:30 (1234) - Jack Harper",
[1] => "FirstTeam 12:00-12:30 (5678) - Sofia Jackson",
[2] => "SecondTeam 12:30-13:00 (1122) - Jack Harper",
[3] => "SecondTeam 12:30-13:00 (2211) - Sofia Jackson"
)

After sort

Array(
[0] => "FirstTeam 12:00-12:30 (1234) - Jack Harper",
[1] => "SecondTeam 12:30-13:00 (1122) - Jack Harper",
[2] => "FirstTeam 12:00-12:30 (5678) - Sofia Jackson",
[3] => "SecondTeam 12:30-13:00 (2211) - Sofia Jackson"
)
share|improve this question
 
Can you change the arrays? So you have coachname and time (also) as a seperate value in the array. –  putvande Nov 27 at 14:27
 
Come on... it's not so difficult, try it yourself... :-) I't just some strings evaluation inside the sort function... –  MarcoS Nov 27 at 14:30
 
How have you tried to tackle this problem? Do you have access to your data not in concatenated string form? –  afuzzyllama Nov 27 at 14:30
add comment

3 Answers

up vote 2 down vote accepted

You can use handcrafted compare function for that to supply to PHP's usort function.

$a = array(
           "FirstTeam 12:00-12:30 (1234) - Jack Harper",
           "FirstTeam 12:00-12:30 (5678) - Sofia Jackson",
           "SecondTeam 12:30-13:00 (1122) - Jack Harper",
           "SecondTeam 12:30-13:00 (2211) - Sofia Jackson"
           );

function cmpCoachTime($s1, $s2) {
  $pattern = '/(\d{2}:\d{2}).*? - ([\w\s]+)/';
  preg_match($pattern, $s1, $matches);
  $coach1 = $matches[2];
  $time1 = strtotime($matches[1]);
  preg_match($pattern, $s2, $matches);
  $coach2 = $matches[2];
  $time2 = strtotime($matches[1]);
  $coachCmp = strcmp($coach1, $coach2);
  if ($coachCmp == 0) {
    if ($time1 == $time2) {
      return 0;
    }
    return $time1 < $time2 ? -1 : 1;
  }
  return $coachCmp;
}

var_dump($a); // outputs initial array

usort($a, 'cmpCoachTime');

var_dump($a); // ouputs sorted array

Tested on your inputs and got desired output.

share|improve this answer
add comment

You can find an example in the PHP docs, use array_multisort:

Link: http://www.php.net/manual/en/function.array-multisort.php

Example of usage:

<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);



// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

?>

and the sorted result:

volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7

Im not sure there are other / better options, but this is the first i found :).

Good luck, Stefan.

share|improve this answer
 
This won't work on OP's array I think since it is all 1 value. –  putvande Nov 27 at 14:29
 
It's not very clear how his data is saved, if it's all 1 value the sorting wouldn't be possible. It should be saved in objects anyway (OOP) :). –  Stefan Koenen Nov 27 at 14:31
1  
@putvande - perhaps the OP can figure out how to split the string into a more sort friendly data structure and then utilize this answer. –  afuzzyllama Nov 27 at 14:31
 
@StefanKoenen array_multisort wouldn't work on objects, though? –  h2ooooooo Nov 27 at 14:32
 
That's not what i ment. I ment if the data is saved in some objects he can split the data the way the example provided above wants :), thats easier i think. –  Stefan Koenen Nov 27 at 14:34
add comment

I think the right structure here would be an array of associative arrays...

[
    [
        'teamName' => 'FirstTeam',
        'startTime' => '12:00',
        'endTime' => '12:30',
        'id' => '1234',
        'coachName' => 'Jack Harper',
    ],
    ...
];

It would be much easier to sort it... :-)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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