1

I have 4 different objects but each have different properties for their dates.

$obj_one->date_added
$obj_two->date_used
$obj_three->transaction_date
$obj_four->credit_date

What I've been trying to do is use array_merge, then array_multisort in order to sort them.

Now, this works if there's two objects, but when there's 4 I am unable to find a way to do it.

This is what I've used for two objects:

$data = array_merge($obj_one, $obj_two);

foreach($data as $key => $row) {
    $obj_one_sort[$key] = $row->date_added;
    $obj_two_sort[$key] = $row->date_used;
}

array_multisort($obj_one_sort, SORT_ASC, $obj_two_sort, SORT_ASC, $sorted_objects);

Any ideas?

EDIT:

This is how I ended up doing it.

$actions = array_merge($object1, $object2, $object3, $object4);

function cmp($a, $b)
    {
        switch(get_class($a)) {
            case 'Object1':
                $date_a = $a->date_added;
                break;
            case 'Object2':
                $date_a = $a->credit_date;
                break;
            case 'Object3':
                $date_a = $a->transaction_date;
                break;
            case 'Object4':
                $date_a = $a->date_used;
                break;
        }

        switch(get_class($b)) {
            case 'Object1':
                $date_b = $b->date_added;
                break;
            case 'Object2':
                $date_b = $b->credit_date;
                break;
            case 'Object3':
                $date_b = $b->transaction_date;
                break;
            case 'Object4':
                $date_b = $b->date_used;
                break;
        }

        if ($date_a == $date_b) {
            return 0;
        }
        return ($date_a < $date_b) ? -1 : 1;
    }

    usort($actions, "cmp");
Karl
  • 5,080
  • 10
  • 41
  • 65
  • Do a comparison method which figures out for each object individually which property applies, then return the comparison value of the two. Please read the duplicate. – deceze Sep 08 '14 at 13:26
  • Well using usort would work, but that would only work for 2 objects not 4, right? Since the callback only takes 2 values. – Karl Sep 08 '14 at 13:38
  • You have an *array* of 4 different objects, no? That's how I understood it. – deceze Sep 08 '14 at 13:51
  • Yes, I have an array with an infinite amount of objects within, but there are only 4 different types of objects. If that makes sense. I need to then sort this array of objects by date, but each object could have one of the 4 variations of property name (date_added, date_used, transaction_date, credit_date). How would this be achievable? – Karl Sep 08 '14 at 14:02
  • Have you read and understood the explanation of how `usort` works exactly and what a comparison function does? http://stackoverflow.com/a/17364128/476 – deceze Sep 08 '14 at 14:03
  • I've read it, but I'm not quite sure how it would fit with what I'm trying to achieve. – Karl Sep 08 '14 at 14:11
  • In your `function cmp($a, $b)`, for both `$a` and `$b`, figure out whether you need to use `$a->date_added` or `$a->date_used` etc., do the same for `$b`. Then return the comparison value of those two properties. – deceze Sep 08 '14 at 14:13
  • Just realised that @deceze, it's a pretty messy way of doing it at the moment, but the actual sorting worked fine. Thanks! – Karl Sep 08 '14 at 14:20
  • I've edited my question with the code which I'm using for usort now, is there a better/cleaner way of doing what I'm doing at the moment? – Karl Sep 08 '14 at 14:32
  • Nope, that's pretty much it. To "de-mess" that you'd have to start with cleaning up your data structure; given the circumstances this is about the best sorting function you can get. – deceze Sep 08 '14 at 14:53
  • Okay thanks for all of your help @deceze – Karl Sep 08 '14 at 15:26
  • Thanks, this helped me a lot. My issue was that I wasn't redefining the dates for `$b`. I was only doing `$a`. – BarryMode Feb 17 '17 at 15:54

0 Answers0