'WPF sort datagrid column of datetimes by TimeOfDay

How can I sort datagrid column by the time of day? I have two columns, for date and time:

<DataGridTextColumn Header="Date" Binding="{Binding Datetime, StringFormat='dddd, d. M. yyyy'}"/>

<DataGridTextColumn Header="Time" Binding="{Binding Datetime, StringFormat='H:mm'}" SortMemberPath="TimeOfDay"/>

When I try to sort it without SortMemberPath it sorts it like a whole datetime. But when I added SortMemberPath="TimeOfDay", it does not sort it at all. I tried SortMemberPath="{Binding TimeOfDay}" and SortMemberPath="{Binding Datetime.TimeOfDay}" too, but it still sorted it like whole datetime.

For example, I have data like this:

Date .................................................. Time

Friday, 1.3.2020 ............................ 14:00

Thursday, 7.3.2020 ...................... 11:30

Sunday, 3.3.2020 ......................... 19:00

Tuesday, 5.3.2020 ....................... 12:15

.

And when sorted by Time I want this:

Date .................................................. Time

Thursday, 7.3.2020 ...................... 11:30

Tuesday, 5.3.2020 ....................... 12:15

Friday, 1.3.2020 ............................ 14:00

Sunday, 3.3.2020 ......................... 19:00

.

But instead I got this:

Date .................................................. Time

Friday, 1.3.2020 ............................ 14:00

Sunday, 3.3.2020 ......................... 19:00

Tuesday, 5.3.2020 ....................... 12:15

Thursday, 7.3.2020 ...................... 11:30



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source