简体   繁体   中英

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? :-)

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())

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.

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

怎么样

nodes.Sort((x,y) => Math.Abs((DateTime.Now, DateTime.Parse(x.GetProperty ("date").Value).Ticks))
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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM