I want to sort some dates numerically (not Date.compare())
Into what type should I convert the dates, so I could sort a table on client side (JS)?
int? timestamp?
and how?
I want to sort some dates numerically (not Date.compare())
Into what type should I convert the dates, so I could sort a table on client side (JS)?
int? timestamp?
and how?
Use DateTime.Ticks
it is of long
type.
Use the Ticks property to get a numerical representation of a DateTime. Here's a sample program that sorts them by Ticks:
static void Main(string[] args)
{
var dates = new List<DateTime> { new DateTime(2011, 5, 31), new DateTime(2012, 7, 31), new DateTime(2010, 1, 31) };
dates.OrderBy(d => d.Ticks).ToList().ForEach(d => Console.WriteLine(d.ToString()));
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
which produces this output:
1/31/2010 12:00:00 AM
5/31/2011 12:00:00 AM
7/31/2012 12:00:00 AM
Press ENTER to exit...
You do not need to covert date to anything to sort it:
new[] { DateTime.Now, DateTime.Now.AddDays(-1) }.OrderBy(d => d);
Just sort it like this
Array.Sort(datetimearr[]);
Use this How to sort ArrayList of DateTime objects in descending order?
Just add them to a collection based on IEnumerable<DateTime>
and use LINQ to sort them, something like:
using System.Collections.Generic;
using System.Linq
...
List<DateTime> dates = new List<DateTime>();
dates.Add(new DateTime(2012, 04, 01));
dates.Add(new DateTime(2012, 04, 05));
dates.Add(new DateTime(2012, 04, 04));
dates.Add(new DateTime(2012, 04, 02));
dates.Add(new DateTime(2012, 04, 03));
List<DateTime> orderedDates = dates.OrderBy(d => d);
You shouldn't need to use DateTime.Ticks
as dates are comparible.