Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy two fields (one in reverse)

I would like to order a friend list by status (online firsts, offline lasts) and by alphabetical order. All I manage to get is:

  • Online firsts / Reversed alphabetical order
  • Or offline firsts / Alphabetical order

Here is plunker to expose my problem

like image 615
JMaylin Avatar asked Nov 29 '13 16:11

JMaylin


People also ask

Can you ORDER BY 2 things in SQL?

After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.

Can we use two columns in ORDER BY clause?

By using ORDER BY clause, we can sort the result in ascending or descending order. This clause can be used with multiple columns as well.

How does sorting by two fields work?

To sort by multiple columns, simply specify the column names separated by commas (just as you do when you are selecting multiple columns). The following code retrieves three columns and sorts the results by two of them—first by price and then by name.


1 Answers

Change the orderBy filter to this:

orderBy:['-status','name'] 

This will order by descending status (by prefixing the - character), then ascending name. Currently you're passing true to reverse the sort, which is causing the status to be correct (online first), but the names to be reversed (i.e., descending).

If you want to keep the reverse boolean, you could use orderBy:['status','-name']:true but that seems less clear than just making status descending as shown earlier.

like image 66
Ahmad Mageed Avatar answered Sep 22 '22 03:09

Ahmad Mageed