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

Sort shortdate doesn't work

1 Answer 251 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Maurice
Top achievements
Rank 1
Veteran
Maurice asked on 11 Sep 2020, 11:39 AM

 

I have these two variables

public DateTime? StartDate { get; set; }
public string StartDateShortDate => StartDate?.ToShortDateString();

 

If I filter on StartDate the sorting start with 2016 then 2017 then 2018 ... like expected.

If I filter on StartDateShortDate the sorting is at "random".

Maurice

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 11 Sep 2020, 03:13 PM

Hi Maurice,

The sort rules for strings differ from those for numbers and from those for dates, so the behavior is expected to be different. I'm attaching below a short video that shows the expected and correct behavior of the string sort operation and the difference between sorting a real DateTime. It results from the code snippet below.

If you need a display for the dates, I would, however, recommend two other options that will also save you the extra string field in the model:

Code snippet for sorting example:

 

<TelerikGrid Data="@MyData" Sortable="true" AutoGenerateColumns="true" Pageable="true">
</TelerikGrid>

@code {
    public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
    {
        Id = x,
        TheName = "name " + x,
        StartDate = DateTime.Now.Date.AddDays(-x)
    });

    public class SampleData
    {
        public int Id { get; set; }
        public string TheName { get; set; }
        public DateTime? StartDate { get; set; }
        public string StartDateShortDate => StartDate?.ToShortDateString();
    }
}

 

 

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Maurice
Top achievements
Rank 1
Veteran
Answers by
Marin Bratanov
Telerik team
Share this question
or