Answered by:
Sorting DateTime property

Question
-
User2074931137 posted
The sort below on DateTime property is incorrect (order by or order by descending does not seem to be working).
Any ideas on what I might be doing wrong would be greatly appreciated.
[DataType(DataType.Time)] [Column(TypeName = "DateTime2")] [DisplayFormat(DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)] [Display(Name = "AM Report")] public DateTime? ContactReportAM { get; set; }
switch (sortOrder) { default: assignments = assignments.OrderBy(s => s.Contact.ContactReportAM); break; case "reportam_desc": assignments = assignments.OrderByDescending(s => s.Contact.ContactReportAM); break;
http://fvm.azurewebsites.net/Assignment/ContactReportAM (default sort)
AM Report Employee Vehicle Down Route Dan D. 2186 I 06:28 Millie 821 04:00 Robert M. 827 05:45 David 23 / 119 05:50 Richard T. 2220 DD 06:01 Robert G. 49 / 145 06:04 Dan K. 2386 34 06:05 Bob B. 2218 DVR K 06:06 Ana 2224 JJ 06:06 Walter 818 06:07 Robert S. 06:07 Tom 2211 32 06:08 Georgiana 816 06:11 Felix 828 06:12 Teresa 829 06:12 Jill 2385 J 06:17 Steve 817 06:19 Arthur 2183 U 06:20 Irene 06:21 Marie P. 830 06:22 Cynthia 815 06:22 Gary G. 48 / 144 06:23 Ralph F. 826 06:23 Rosemary 820 06:25 Bob G. 2186 I 06:27 Richard G. 2182 Y 06:29 AJ 2219 DVR T 06:29 Duvert 2388 V 06:29 Roman 2239 D 06:29 Peter 824 06:29 Roman 06:31 Lennard 2234 P 06:31 Joe L. 2389 L 06:31 Dan B. 2223 N 06:32 Sandy 2229 O 06:32 Lewis 819 06:34 Jean (GiGi) 2244 S 06:34 Bill 2250 M 06:34 Ray C. 2387 X 06:34 Ralph A. 813 06:37 Ed 28 / 124 06:39 Mike 809 06:39 Dan T. 812 06:49 Emily 808 06:59 Sal L. R 07:00 Ray S. 814 07:15 Jean 810 07:55 Lorna 811 06:23 Bob W. 823 06:32 Anthony Tech http://fvm.azurewebsites.net/Assignment/ContactReportAM?sortOrder=reportam_desc
AM Report Employee Vehicle Down Route 06:32 Anthony Tech 06:23 Bob W. 823 07:55 Lorna 811 07:15 Jean 810 07:00 Ray S. 814 06:59 Sal L. R 06:49 Emily 808 06:39 Dan T. 812 06:39 Mike 809 06:37 Ed 28 / 124 06:34 Ralph A. 813 06:34 Bill 2250 M 06:34 Jean (GiGi) 2244 S 06:34 Ray C. 2387 X 06:32 Lewis 819 06:32 Sandy 2229 O 06:31 Lennard 2234 P 06:31 Joe L. 2389 L 06:31 Dan B. 2223 N 06:29 AJ 2219 DVR T 06:29 Duvert 2388 V 06:29 Roman 2239 D 06:29 Peter 824 06:29 Roman 06:27 Richard G. 2182 Y 06:25 Bob G. 2186 I 06:23 Ralph F. 826 06:23 Rosemary 820 06:22 Cynthia 815 06:22 Gary G. 48 / 144 06:21 Marie P. 830 06:20 Irene 06:19 Arthur 2183 U 06:17 Steve 817 06:12 Jill 2385 J 06:12 Teresa 829 06:11 Felix 828 06:08 Georgiana 816 06:07 Tom 2211 32 06:07 Robert S. 06:06 Ana 2224 JJ 06:06 Walter 818 06:05 Bob B. 2218 DVR K 06:04 Dan K. 2386 34 06:01 Robert G. 49 / 145 05:50 Richard T. 2220 DD 05:45 David 23 / 119 04:00 Robert M. 827 06:28 Millie 821 Dan D. 2186 I Saturday, March 16, 2019 3:59 PM
Answers
-
User475983607 posted
My best guess the report spans more than one day and therefore the sort is working as expected.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 16, 2019 4:25 PM
All replies
-
User475983607 posted
My best guess the report spans more than one day and therefore the sort is working as expected.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, March 16, 2019 4:25 PM -
User2074931137 posted
https://forums.asp.net/p/2153881/6255132.aspx?p=True&t=636883309140915404
Saturday, March 16, 2019 6:02 PM -
User1520731567 posted
Hi 3v3rhart,
It seems that OrderByDescending or OrderBy can not be accurate to the time(hh mm ss).
You could refer to this link:
https://stackoverflow.com/a/31650314
It isn't pretty at all, but it get's the job done.
Or I suggest you could refer to this link:
using the Date and TimeOfDay properties for the sort:
.OrderByDescending(c=> c.Time.Date).ThenBy(c=> c.Time.TimeOfDay);
or change the other way to achieve the goal:
https://stackoverflow.com/questions/1433088/asp-net-mvc-model-orderby-date-has-no-effect
.OrderByDescending(a => a.Date).ThenBy(a => a.Id).ToList();
Hope my reply will be helpful to you.
Best Regards.
Yuki Tao
Monday, March 18, 2019 7:04 AM