Default Sort Order

Jan 15, 2014 at 2:45 PM
What is the intended method for controlling the initial sort order?

I understand "SortInitialDirection" can be applied to a column if the model collection is already sorted. But doesn't using it imply that the collection will get sorted twice? Once in the hard coded default order and then again by the "orderers" created by clicking on columns ("_settings.ColumnName") and any "ThenSortBy" methods?

What I'm looking for is a way to set a default value for "_settings.ColumnName". But as far as I've discovered, it only gets set in response to the query string.
Jan 15, 2014 at 4:20 PM
Edited Jan 15, 2014 at 4:27 PM
Goal:
Initialize the grid as if "?grid-column=WhenModified&grid-dir=0" had been added to query string.
i.e. Add "SortDefaults" as a new grid customization method.
.WithPaging(15).Sortable().SortDefaults("WhenModified", GridSortDirection.Descending).Filterable().WithMultipleFilters()
Code changes:
Columns/GridColumnCollection.cs
- internal void UpdateColumnsSorting()
public void UpdateColumnsSorting()
Columns/IGridColumnCollection.cs
/// <summary>
/// Call to update column IsSorted and Direction properties from SortSettings
/// </summary>
void UpdateColumnsSorting();
Html/GridHtmlOptions.cs
public IGridHtmlOptions<T> SortDefaults(string columnName, GridSortDirection direction) {
    var s = _source.Settings.SortSettings;
    if (string.IsNullOrEmpty(s.ColumnName)) {
        s.ColumnName = columnName;
        s.Direction = direction;
        _source.Columns.UpdateColumnsSorting();
    }
    return this;
}
Html/GridHtmlOptions.cs
/// <summary>
/// Provides default sort column and direction to be used when none are provided
/// on the query string.
/// </summary>
/// <param name="columnName"></param>
/// <param name="direction"></param>
/// <returns></returns>
IGridHtmlOptions<T> SortDefaults(string columnName, GridSortDirection direction);
Coordinator
Jan 17, 2014 at 3:35 AM
Hi,

I understand that you care about twice OrderBy. Now grid does not have an api to setup default order operation, and I guess that easy to implement. But what problems you are having with that? Any performance issues?

Also if you are fetch you data from database and using IQueryable<T> interface, provided by EntityFramework - double order will not be executed on a database side.
Jan 17, 2014 at 4:38 AM
I may not fully understand how it's intended to work but I'm concerned about the performance against large tables.

The sort has to happen on the SQL server so that the paging (skip M, take N) can also happen there.

Otherwise a potentially enormous amount of data needs to be transferred to the web server only to immediately be resorted and mostly discarded.
Jan 17, 2014 at 2:16 PM
The code changes I listed above are working for me.
I was asking to know if I was missing something about how it was meant to work.

In general, looking at the SQL being generated and sent to the server I'm happy with the efficiency.
(looking at the value of itemsToProcess.ToString() at line 98 in GridBase.cs)
Coordinator
Jan 18, 2014 at 3:59 AM
If you use QueryProvider by EntityFramework (DbSet<T>) with the grid you don't need to care about double sorting because expression like:
sourceItems.OrderBy(x=>x.Field1).OrderBy(x=>x.Field2)
will be translated to SQL statement like:
SELECT [fields] FROM [table] ORDER BY Field2
And in this case you can initial sort you collection. then pass to the grid and use SortInitialDirection method.

With your code customization you will may have a problems to upgrade the grid to the next versions.
Jan 18, 2014 at 9:44 AM
Thanks for this explanation.

That takes care of my efficiency concerns.