0

I am trying to sort eloquent collection using sortByDesc. It works for first sorting but chaining with more columns doesn't give proper result. Here is the query and code I am trying

    $data = Divrank::where('division_id', $id)->with(['team.player1', 'team.player2'])->with('meta')->orderBy('position', 'asc')->get();
    foreach($data as $k=>$t){
        if ($t->meta) { // it has ranks informations
            $t->totalSets = $t->meta->totalSets;
            $t->totalGames = $t->meta->totalGames;
            $t->points = $t->meta->points;
           
        } else {
            $t->totalSets = 0;
            $t->totalGames = 0;
            $t->points = 0;
           
        }
        unset($data[$k]->meta);

    }

    //return $data;
    return $data->sortByDesc('points')->sortByDesc('totalSets')->sortByDesc('totalGames');

If I remove ->sortByDesc('totalSets')->sortByDesc('totalGames') then it shows correct formate.

Any ideas how to achieve this? Thank you.

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95
  • https://stackoverflow.com/questions/25451019/what-is-the-syntax-for-sorting-an-eloquent-collection-by-multiple-columns – Bennett Dec 02 '20 at 14:30

1 Answers1

1

Ok, got it, I need to do reverse sorting. $data->sortByDesc('totalGames')->sortByDesc('totalSets')->sortByDesc('points'); this works. It will sort first with games, then sets and then points..

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95