Home > Software design >  Laravel group collection and sort by the biggest value
Laravel group collection and sort by the biggest value

Time:05-09

I have a Laravel collection with values, which I want to group by 'type' and sort by the biggest 'probability'. Here is how my collection looks like:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [type] => correct-score
                    [name] => 1-1
                    [probability] => 10.599257670442
                    [probability_odd] => 10.142219704667
                    [odd] => 
                    [value_bet] => 0
                    [prognose_type_id] => 12
                    [value_bet_diff] => -10.142219704667
                )

            [1] => Array
                (
                    [type] => 3-way
                    [name] => 1
                    [probability] => 52.459602877727
                    [probability_odd] => 2.0491958402842
                    [prognose_type_id] => 1
                    [odd] => 2.48
                    [value_bet_diff] => 0.4308041597158
                )

            [2] => Array
                (
                    [type] => 3-way
                    [name] => 2
                    [probability] => 24.682032371379
                    [probability_odd] => 4.3553949845985
                    [prognose_type_id] => 1
                    [odd] => 2.66
                    [value_bet_diff] => -1.6953949845985
                )

            [3] => Array
                (
                    [type] => 3-way
                    [name] => X
                    [probability] => 22.857984044926
                    [probability_odd] => 4.7029519221256
                    [prognose_type_id] => 1
                    [odd] => 3.7
                    [value_bet_diff] => -1.0029519221256
                )

            [4] => Array
                (
                    [type] => double-chance
                    [name] => 1X
                    [probability] => 37.658793461327
                    [probability_odd] => 2.8545789739758
                    [prognose_type_id] => 4
                    [odd] => 1.49
                    [value_bet_diff] => -1.3645789739758
                )

            [5] => Array
                (
                    [type] => double-chance
                    [name] => X2
                    [probability] => 23.770008208152
                    [probability_odd] => 4.5225058005294
                    [prognose_type_id] => 4
                    [odd] => 1.55
                    [value_bet_diff] => -2.9725058005294
                )

            [6] => Array
                (
                    [type] => double-chance
                    [name] => 12
                    [probability] => 38.570817624553
                    [probability_odd] => 2.7870811826288
                    [prognose_type_id] => 4
                    [odd] => 1.29
                    [value_bet_diff] => -1.4970811826288
                )

            [7] => Array
                (
                    [type] => both-to-score
                    [name] => yes
                    [probability] => 58.505776168275
                    [probability_odd] => 1.8374254140447
                    [prognose_type_id] => 8
                    [odd] => 
                    [value_bet_diff] => -1.8374254140447
                )

I want to achieve as result something like that:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [type] => correct-score
                    [name] => 1-1
                    [probability] => 10.599257670442
                    [probability_odd] => 10.142219704667
                    [odd] => 
                    [value_bet] => 0
                    [prognose_type_id] => 12
                    [value_bet_diff] => -10.142219704667
                )

            [1] => Array
                (
                    [type] => 3-way
                    [name] => 1
                    [probability] => 52.459602877727
                    [probability_odd] => 2.0491958402842
                    [prognose_type_id] => 1
                    [odd] => 2.48
                    [value_bet_diff] => 0.4308041597158
                )


            [6] => Array
                (
                    [type] => double-chance
                    [name] => 12
                    [probability] => 38.570817624553
                    [probability_odd] => 2.7870811826288
                    [prognose_type_id] => 4
                    [odd] => 1.29
                    [value_bet_diff] => -1.4970811826288
                )

            [7] => Array
                (
                    [type] => both-to-score
                    [name] => yes
                    [probability] => 58.505776168275
                    [probability_odd] => 1.8374254140447
                    [prognose_type_id] => 8
                    [odd] => 
                    [value_bet_diff] => -1.8374254140447
                )

Haven't been able to find similar solutions, so I finally decided to try here. Can you please help me?

Thanks in advance!

CodePudding user response:

I think below can work;

$collection->sortByDesc('probability')->groupBy('type');

Sort by probability than group by type;

CodePudding user response:

Probably better to do this in Eloquent, but as you have presented this as a collections problem;

assuming your collection is called $items

$sorted = $items->groupBy('type')
            ->map(function($group){
              return $group->sortByDesc('probability')->take(1);
            })
            ->flatten(1);

CodePudding user response:

The most short:

$items = $collections
    ->sortByDesc('probability')
    ->unique('type');
  • Related