9/28/2015

This post won't contain any code. I just want to share some basic and well-known things and point to one weird behavior I've found.
So Sorting... or Ordering... or <OrderBy>.
You can add sorting to list view in List Settings -> Edit View in Sort section. There you can add 1 or 2 columns to sort and sorting order. Items will be sorted by the first selected column and then by the second one if they have the same value in the first column.
When user works with the view (browse list items) he can add additional sorting through UI by clicking a header of any column (well, almost any - you can't sort by Multi Lookup columns, User and Group columns with allow multiple selection). In that case selected column will be added as the first item of sorting chain. That means that items will be sort by:

Column that is added by user through List View UI

Columns that were added in Sort section of Edit View page.

This behavior looks good and clear.

BUT IT IS NOT TRUE IF USER ADDS SORTING TO TASK LIST.

If user works with Task list (or descendant of task list) and adds sorting by clicking a column's header (or in context menu of a column's header) all sorting columns that were added to View will be removed. Moreover, sorting by Title will be added. And as a result items will be sort by:

Column that is added by user through List View UI

Title column

I don't know why it is so, but it is so...

Don't miss that fact if you're implementing some features that are dependent on sorting (for example, custom pagination).

If you know some other exceptions to the rule please share them in comments.