Skip to content

TIL you can mix ASC, DESC in ORDER BY (SQLite)

November 4th, 2015 - Software

TL;DR: You can use ORDER BY column1 DESC, column2 ASC … in your code.

The other day I was trying to get a custom ordering in a list for an object:

  • order decreasing by UNREAD MESSAGES count and then
  • order ascending by DATE

Initially, I tried a GROUP BY, but I could not get it to do the ordering properly. Then I thought of composite keys (multiple rows, like ones used in statistics), but then I realised it’d be overkill for a small app.

Then, I found out that you can do per-column sorting in ORDER BY:

SELECT *
FROM applications
ORDER BY unreadCount DESC, publicationDate ASC;

Cool!

HTH,

Share on
Reddit
Linked in
Whatsapp

A little experiment: