0

I want to sort data that is coming from Api resource on the basis of their rating that is coming from the resource. I have been working on the SortByDesc method but it's not giving proper results.

public function reviewlist()
{
    $post = Post::all();
    $list = RatingResource::collection($post);
    
    return  $list->sortByDesc('Rating');
  }

1 Answers1

2

Got an answer that you have to use this method for applying sort on resource data.

public function reviewlist()
{
    $post = Post::all();
    $list = RatingResource::collection($post);
    
    $statisticCollection = collect($list);

    $sorted = $statisticCollection->sortByDesc('Rating');

    return $sorted->values()->all();
 }