简体   繁体   English

如何按声誉和创建日期对集合进行排序

[英]How to sort a collection by reputation and creation date

Assuming I want to sort a list of users by reputation: 假设我想按声誉对用户列表进行排序:

  1. User with 5 reputation 用户有5个声望
  2. User with 3 reputation 用户有3个声望
  3. User with 0 reputation (this user was registered before the 4th user) 声誉为0的用户(此用户在第4个用户之前注册)
  4. User with 0 reputation 用户名誉为0

By doing $query->get()->sortByDesc('reputation') , I receive the following results: 通过$query->get()->sortByDesc('reputation') ,我收到以下结果:

  1. User with 5 reputation 用户有5个声望
  2. User with 3 reputation 用户有3个声望
  3. User with 0 reputation 用户名誉为0
  4. User with 0 reputation (this user was registered before the 4th user) 声誉为0的用户(此用户在第4个用户之前注册)

How can I sort the collection to receive results similar to the first example? 如何对集合进行排序以接收与第一个示例类似的结果? In other words, if users have 0 reputation, how can I change the sorting method to creation date by descending order? 换句话说,如果用户的声誉为0,如何按降序将排序方法更改为创建日期?

在查询中链接两个orderBy()方法:

->orderBy('created_at', 'desc')->orderBy('reputation', 'desc')->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM