bomortensen
bomortensen

Reputation: 3396

Sort list of dates closest to current date

I have to a list of objects which has a DateTime property and I need to sort this list so that the objects with the closest date to DateTime.Now is first in the list.

I have tried the following:

nodes.Sort((x, y) => DateTime.Compare(
         DateTime.Now, 
         DateTime.Parse(x.GetProperty("date").Value)));

But this doesn't return a correct result.

Does anyone know of a good way to do this? :-)

Upvotes: 3

Views: 3891

Answers (5)

eaydemir
eaydemir

Reputation: 47

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        var recs = p.ReadVisitorsByMonthly();
        Console.WriteLine();

        foreach (var r in recs)
        {
            Console.WriteLine(r);
        }

        Console.ReadLine();
    }

    public List<string> ReadVisitorsByMonthly()
    {
        Dictionary<int, int> retL = new Dictionary<int, int>();
        List<DateTime> liste = new List<DateTime>();
        List<string> ret = new List<string>();

        liste.Add(new DateTime(2016, 4, 1));
        liste.Add(new DateTime(2016, 4, 4));
        liste.Add(new DateTime(2016, 4, 5));
        liste.Add(new DateTime(2016, 4, 2));
        liste.Add(new DateTime(2016, 4, 3));
        liste.Add(new DateTime(2015, 11, 6));
        liste.Add(new DateTime(2015, 12, 7));
        liste.Add(new DateTime(2015, 12, 8));
        liste.Add(new DateTime(2015, 11, 4));
        liste.Add(new DateTime(2015, 12, 4));
        liste.Add(new DateTime(2016, 5, 1));
        liste.Add(new DateTime(2016, 5, 6));
        liste.Add(new DateTime(2016, 5, 2));
        liste.Add(new DateTime(2016, 5, 3));
        liste.Add(new DateTime(2016, 2, 8));
        liste.Add(new DateTime(2016, 2, 6));
        liste.Add(new DateTime(2016, 2, 2));
        liste.Add(new DateTime(2016, 2, 1));
        liste.Add(new DateTime(2016, 1, 3));
        liste.Add(new DateTime(2016, 3, 5));
        liste.Add(new DateTime(2016, 3, 4));
        liste.Add(new DateTime(2016, 3, 7));
        liste.Add(new DateTime(2016, 3, 3));
        liste.Add(new DateTime(2016, 3, 5));



        var list = liste.GroupBy(j => j.Month).ToList();

        foreach (var g in list)
        {
            Console.WriteLine(g.Key.ToString("D2") + " - " + g.Count());
            retL.Add(g.Key, g.Count());
        }


        var thisMonth = DateTime.Now.Month;

        var finalList = retL.OrderBy(j => (thisMonth - (j.Key > thisMonth ? j.Key - 12 : j.Key))).ToList();

        foreach (var kvp in finalList)
        {
            string strMonthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(kvp.Key);

            ret.Add(kvp.Key.ToString().PadRight(3) + strMonthName.PadRight(8) + kvp.Value);
        }

        return ret;
    }
}

Upvotes: 0

James
James

Reputation: 82096

Have you tried OrderByDescending?

nodes.OrderByDescending(x => DateTime.Parse(x.GetProperty("date").Value));

Not sure if this would exactly what you are after, but if all you are trying to do is order the list by most recent date this will do the job.

Upvotes: 2

Lee
Lee

Reputation: 144126

You can order them by the absolute difference between the node's time and the current time. You can get the absolute time of a TimeSpan with the Duration method:

DateTime now = DateTime.Now;
var ordered = nodes.OrderBy(n => (now - DateTime.Parse(n.GetProperty("date").Value)).Duration())

Upvotes: 10

Visions
Visions

Reputation: 947

how bout

nodes.Sort((x,y) => Math.Abs((DateTime.Now, DateTime.Parse(x.GetProperty ("date").Value).Ticks))

Upvotes: 0

Dax Fohl
Dax Fohl

Reputation: 10781

from node in nodes
let diff = Math.Abs(DateTime.Now.Subtract(node.DateTime).TotalSeconds)
order by diff
select date

Upvotes: 0

Related Questions