Laravel collection sortBy not taking effect

Titan picture Titan · Mar 12, 2017 · Viewed 9k times · Source

I'm trying to combine and sort the results from several db queries.

$events = collect();

$downedEvents = EventDowned::where('mission', $missionId)
   ->orderBy('mission_time', 'asc')
   ->get();

$events->push($downedEvents);

$getInOutEvents = EventGetInOut::where('mission', $missionId)
   ->orderBy('mission_time', 'asc')
   ->get();

$events->push($getInOutEvents);

$missileEvents = EventMissile::where('mission', $missionId)
   ->orderBy('mission_time', 'asc')
   ->get();

$events->push($missileEvents);

$flattenedEvents = $events->flatten();
$sortedEvents = $flattenedEvents->sortBy('mission_time');

return $sortedEvents->all();

The result looks like this:

ss

As you can see it has correctly combined the results, however they remain in their original query order, not sorted.

I've also tried

$sortedEvents = $flattenedEvents->sortBy(function($event) {
    return (int) $event->mission_time;
});

Answer

Titan picture Titan · Mar 13, 2017

A huge fail on my part, my pretty print JSON chrome extension was messing with the display order, viewing the raw response showed they were in fact sorted correctly... ::facepalm::