简体   繁体   中英

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
  2. User with 3 reputation
  3. User with 0 reputation (this user was registered before the 4th user)
  4. User with 0 reputation

By doing $query->get()->sortByDesc('reputation') , I receive the following results:

  1. User with 5 reputation
  2. User with 3 reputation
  3. User with 0 reputation
  4. User with 0 reputation (this user was registered before the 4th user)

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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