【已解决】这个方法不会写啊,求高手相助

wocall

问题描述

下面数组已经按gross_total排名,rank为名次。

但是当gross_total值相等时,名次也相等了,程序希望相同名次里也要排名,比如3个第三名,要排出3、4、5名来。

相同名次排名的数据从每个用户的子数组scores中来比较,即先比较最后一条数据的gross,谁的数值小,谁就排前面;如果最后一行的gross也相等,那就倒数第二条,依此类推,直到排出一个先后来。

要将数组里所有排名重复的用户都要排出唯一的名次来,名次赋值给rank。

哪位大佬能帮我出出主意,如何写这个函数么?
或者有偿服务也可。

```php
$users=[
{"userid":15907,"gross_total":75,"scores":[{"scoreid":61691813,"gross":5},{"scoreid":61691814,"gross":4},{"scoreid":61691815,"gross":5},{"scoreid":61691816,"gross":3},{"scoreid":61691817,"gross":6},{"scoreid":61691818,"gross":5},{"scoreid":61691819,"gross":4},{"scoreid":61691820,"gross":2},{"scoreid":61691821,"gross":4}],"rank":"1"},
{"userid":87004,"gross_total":75,"scores":[{"scoreid":61811621,"gross":5},{"scoreid":61811622,"gross":5},{"scoreid":61811623,"gross":4},{"scoreid":61811624,"gross":3},{"scoreid":61811625,"gross":3},{"scoreid":61811626,"gross":4},{"scoreid":61811627,"gross":5},{"scoreid":61811628,"gross":3},{"scoreid":61811629,"gross":6}],"rank":"1"},
{"userid":77801,"gross_total":77,"scores":[{"scoreid":61856900,"gross":5},{"scoreid":61856901,"gross":6},{"scoreid":61856902,"gross":5},{"scoreid":61856903,"gross":4},{"scoreid":61856904,"gross":4},{"scoreid":61856905,"gross":5},{"scoreid":61856906,"gross":4},{"scoreid":61856907,"gross":3},{"scoreid":61856908,"gross":5}],"rank":3},
{"userid":199466,"gross_total":78,"scores":[{"scoreid":61735175,"gross":5},{"scoreid":61735176,"gross":5},{"scoreid":61735177,"gross":4},{"scoreid":61735178,"gross":3},{"scoreid":61735179,"gross":4},{"scoreid":61735180,"gross":4},{"scoreid":61735181,"gross":3},{"scoreid":61735182,"gross":3},{"scoreid":61735183,"gross":5}],"rank":4},
{"userid":40921,"gross_total":79,"scores":[{"scoreid":61691795,"gross":4},{"scoreid":61691796,"gross":8},{"scoreid":61691797,"gross":4},{"scoreid":61691798,"gross":3},{"scoreid":61691799,"gross":4},{"scoreid":61691800,"gross":4},{"scoreid":61691801,"gross":5},{"scoreid":61691802,"gross":3},{"scoreid":61691803,"gross":7}],"rank":5},
{"userid":132135,"gross_total":81,"scores":[{"scoreid":61763210,"gross":4},{"scoreid":61763211,"gross":4},{"scoreid":61763212,"gross":4},{"scoreid":61763213,"gross":4},{"scoreid":61763214,"gross":4},{"scoreid":61763215,"gross":4},{"scoreid":61763216,"gross":6},{"scoreid":61763217,"gross":4},{"scoreid":61763218,"gross":7}],"rank":6},
{"userid":6501,"gross_total":82,"scores":[{"scoreid":61693505,"gross":5},{"scoreid":61693506,"gross":5},{"scoreid":61693507,"gross":3},{"scoreid":61693508,"gross":4},{"scoreid":61693509,"gross":4},{"scoreid":61693510,"gross":4},{"scoreid":61693511,"gross":5},{"scoreid":61693512,"gross":3},{"scoreid":61693513,"gross":6}],"rank":7},
{"userid":1695,"gross_total":83,"scores":[{"scoreid":61762688,"gross":5},{"scoreid":61762689,"gross":5},{"scoreid":61762690,"gross":6},{"scoreid":61762691,"gross":5},{"scoreid":61762692,"gross":4},{"scoreid":61762693,"gross":6},{"scoreid":61762694,"gross":4},{"scoreid":61762695,"gross":3},{"scoreid":61762696,"gross":5}],"rank":8},
{"userid":453,"gross_total":85,"scores":[{"scoreid":61761986,"gross":5},{"scoreid":61761987,"gross":5},{"scoreid":61761988,"gross":5},{"scoreid":61761989,"gross":3},{"scoreid":61761990,"gross":3},{"scoreid":61761991,"gross":7},{"scoreid":61761992,"gross":5},{"scoreid":61761993,"gross":3},{"scoreid":61761994,"gross":6}],"rank":"9"},
{"userid":9625,"gross_total":85,"scores":[{"scoreid":61737839,"gross":6},{"scoreid":61737840,"gross":4},{"scoreid":61737841,"gross":4},{"scoreid":61737842,"gross":3},{"scoreid":61737843,"gross":4},{"scoreid":61737844,"gross":5},{"scoreid":61737845,"gross":5},{"scoreid":61737846,"gross":4},{"scoreid":61737847,"gross":6}],"rank":"9"},
{"userid":148887,"gross_total":85,"scores":[{"scoreid":61811657,"gross":5},{"scoreid":61811658,"gross":6},{"scoreid":61811659,"gross":7},{"scoreid":61811660,"gross":3},{"scoreid":61811661,"gross":5},{"scoreid":61811662,"gross":6},{"scoreid":61811663,"gross":4},{"scoreid":61811664,"gross":4},{"scoreid":61811665,"gross":6}],"rank":"9"},
{"userid":445679,"gross_total":86,"scores":[{"scoreid":61834175,"gross":3},{"scoreid":61834176,"gross":5},{"scoreid":61834177,"gross":5},{"scoreid":61834178,"gross":3},{"scoreid":61834179,"gross":4},{"scoreid":61834180,"gross":8},{"scoreid":61834181,"gross":4},{"scoreid":61834182,"gross":3},{"scoreid":61834183,"gross":6}],"rank":12},
{"userid":296,"gross_total":87,"scores":[{"scoreid":61762004,"gross":4},{"scoreid":61762005,"gross":6},{"scoreid":61762006,"gross":4},{"scoreid":61762007,"gross":3},{"scoreid":61762008,"gross":5},{"scoreid":61762009,"gross":5},{"scoreid":61762010,"gross":4},{"scoreid":61762011,"gross":3},{"scoreid":61762012,"gross":6}],"rank":"13"},
{"userid":13433,"gross_total":87,"scores":[{"scoreid":61762022,"gross":5},{"scoreid":61762023,"gross":6},{"scoreid":61762024,"gross":4},{"scoreid":61762025,"gross":3},{"scoreid":61762026,"gross":5},{"scoreid":61762027,"gross":4},{"scoreid":61762028,"gross":4},{"scoreid":61762029,"gross":3},{"scoreid":61762030,"gross":5}],"rank":"13"},
{"userid":48268,"gross_total":88,"scores":[{"scoreid":61691957,"gross":6},{"scoreid":61691958,"gross":4},{"scoreid":61691959,"gross":4},{"scoreid":61691960,"gross":4},{"scoreid":61691961,"gross":5},{"scoreid":61691962,"gross":5},{"scoreid":61691963,"gross":4},{"scoreid":61691964,"gross":3},{"scoreid":61691965,"gross":7}],"rank":"15"},
{"userid":102589,"gross_total":88,"scores":[{"scoreid":61693523,"gross":5},{"scoreid":61693524,"gross":6},{"scoreid":61693525,"gross":5},{"scoreid":61693526,"gross":4},{"scoreid":61693527,"gross":3},{"scoreid":61693528,"gross":4},{"scoreid":61693529,"gross":5},{"scoreid":61693530,"gross":4},{"scoreid":61693531,"gross":5}],"rank":"15"},
{"userid":8770,"gross_total":89,"scores":[{"scoreid":61714376,"gross":5},{"scoreid":61714377,"gross":5},{"scoreid":61714378,"gross":4},{"scoreid":61714379,"gross":4},{"scoreid":61714380,"gross":4},{"scoreid":61714381,"gross":7},{"scoreid":61714382,"gross":3},{"scoreid":61714383,"gross":5},{"scoreid":61714384,"gross":5}],"rank":17}]

问了半天AI,这几天chatGPT要死不活的,回答问题也是答非所问。。。。。头疼。
998 4 1
4个回答

不败少龙

是不是需要多个orderBy

  • 暂无评论
小W

不知道数据库是如何设计的,但是可以将数据查出来之后,在代码中排序就很简单了

  • 暂无评论
hon陈烁临

可以自己实现排序算法,逻辑你已经说清楚了,关键就是进行多次循环排序。
这里我推荐你使用 laravel的集合进行排序,自定义一下排序字段就行如下

function grossSort(string|array $users)
{
    $gArr = is_array($users) ? $users : json_decode($users, true);
    $sortFn = ['gross_total'];
    $sortSubArr = $gArr[0]['scores'];
    krsort($sortSubArr);
    foreach ($sortSubArr as $k =>  $vArr) {
        $sortFn[] = fn (array $a, array $b) => $a['scores'][$k]['gross'] <=> $b['scores'][$k]['gross'];
    }
    $sorted  = (new \Illuminate\Support\Collection($gArr))->sortBy($sortFn)->values()->all();
    return $sorted;
}
  • hon陈烁临 2023-10-31

    如果你的项目里面没有 Illuminate\Support\Collection 可以使用 composer require -W illuminate/support 安装

wocall

感谢楼上各位,受启发,终于写出来了:

        usort($data, function ($a, $b) {
            if($a['gross_total'] == $b['gross_total']){
                for($i = count($a['scores'])-1; $i > 0; $i--){
                    if($a['scores'][$i]['gross'] == $a['scores'][$i]['gross']){
                        continue;
                    }else{
                        return $a['scores'][$i]['gross'] > $a['scores'][$i]['gross'] ? 1 : -1;
                    }
                }
            }else{
                return $a['gross_total'] > $b['gross_total'] ? 1 : -1;
            }
        });
  • 暂无评论
年代过于久远,无法发表回答
×
🔝