This is a migrated thread and some comments may be shown as answers.

Sorting ShortDateStrings properly

1 Answer 376 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 11 Oct 2018, 04:05 PM

Hello, in my grid I stored the DateTime as a short date string (Format: "month/day/year") and of course, it is sorting improperly. I looked up other solutions and they are for other platforms. From what I've seen you guys have a javascript library where it will properly sort the column. What is that javascript and how do I specify the correct column that needs to be sorted like a date? Do I also need to specify what format it is in? This is my code:

I want to sort c.ReceivedDate as a date.

@(Html.Kendo().Grid<ViewModels.RecentReceiptsViewModel>()
            .Name("RecentReceiptsGrid")
            .Columns(columns =>
            {
                columns.Bound(c => c.PoNumber).Title("Po#").Width(100);
                columns.Bound(c => c.ReceivedDate).Title("Date").Width(100);
                columns.Bound(c => c.ItemName).Title("Item").Width(100);
                columns.Bound(c => c.Dimensions).Width(100);
                columns.Bound(c => c.Cost).Width(100);
                columns.Bound(c => c.Quantity).Width(100);
                columns.Bound(c => c.Total).Width(100);
            })
            .Scrollable()
            .Groupable()
            .Sortable()
            .DataSource(
            dataSource => dataSource.Ajax()
            .Read(read => read.Action("ReadRecentReceiptsReport", "Reports")))

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 16 Oct 2018, 10:11 AM
Hello Jason,

When dates are stored as strings sorting them would work differently as the values will be sorted in alphabetical order. In order to have the dates sorted chronologically out of the box I suggest storing the dates as DateTime. Then you can only change the format in which the dates are displayed via the Format() option for the column. Check out the OrderDate column in the example below that illustrates the approach:


With that said, in order to apply custom sort order you can enable ServerOperations for the Grid and implement the sorting server-side. 


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or