datetime - Trouble Sorting a DATE column in DataTable/DataView in C# -
i trying sort dataview in c#. seems sort method of dataview class takes dates strings.
in output getting -
5/9/2013 4:56:38 pm 5/8/2013 4:23:06 pm 5/8/2013 1:38:21 pm 5/7/2013 9:55:30 pm 5/7/2013 7:54:45 pm 5/7/2013 7:44:10 pm 5/7/2013 7:44:10 pm 5/7/2013 12:26:38 pm 5/7/2013 1:44:06 pm 5/6/2013 4:08:54 pm 5/6/2013 10:32:49 5/4/2013 7:54:23 pm 5/4/2013 12:57:21 pm 5/3/2013 3:49:03 pm 5/3/2013 3:49:03 pm 5/3/2013 2:06:12 pm 5/3/2013 11:19:34 5/3/2013 11:03:32 5/3/2013 1:58:38 pm 5/2/2013 7:27:55 pm 5/2/2013 7:17:50 pm 5/2/2013 7:06:06 pm 5/2/2013 6:42:37 pm 5/2/2013 6:30:58 pm 5/13/2013 12:49:24 pm this code.
datatable dt; dataview dv = dt.defaultview; dv.sort = "messagedate desc"; datatable sorteddt = dv.totable(); foreach (datarow row in sorteddt.rows) { code print. } as can see last date 5/13/2013 should @ first , not @ bottom 5/13 > 5/9 if date comparison, 5/13<5/9 if take string.
messagedate column datetime in declaration, still compiler converting string.
public struct messages { public string profileid { get; set; } public string network { get; set; } public string fromid { get; set; } public string fromname { get; set; } public string fromprofileurl { get; set; } public datetime messagedate { get; set; } public string message { get; set; } public string fbcomment { get; set; } public string fblike { get; set; } public string messageid { get; set; } public string type { get; set; } } any ideas why happening , how can workaround same?
you need implement icomparable sort datetime: http://forums.asp.net/p/1267353/2393006.aspx
alternatively, if can change struct, can add sorting value:
public string sortvalue { { return ((int)((messagedate - new datetime(1970, 1, 1)).totalseconds)).tostring("d12"); } } which converts messagedate seconds-since-epoch value, , can lexicographically sorted.
Comments
Post a Comment