2013-07-17 93 views
1

我必须列出具有DateTime属性的对象,并且我需要对此列表进行排序,以便与列表中最接近的日期对象为DateTime.Now排序最接近当前日期的日期列表

我曾尝试以下:

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

但这不返回正确的结果。

有没有人知道一个很好的方法来做到这一点? :-)

+1

你能不只是'OrderByDescending'的日期? –

+1

[第一个谷歌结果为“排序日期时间c#”](http://www.dotnetperls.com/sort-datetime) - hmm ... – Sayse

+2

你的意思是说你想按照abs(日期 - 现在)排序, ? –

回答

7

您可以通过节点时间和当前时间之间的绝对差值对它们进行排序。你可以得到一个TimeSpan的绝对时间与Duration方法:

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

This!这正是我所需要的! :-)非常感谢,李。 – bomortensen

2

你试过OrderByDescending

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

不知道这是否会正是你后,但如果你正在试图做的是顺序列表中最近的日期,这将做的工作是什么。

+0

你的答案假定未来没有日期。 – Rotem

+1

@Rotem它不会*假设*任何东西,我已经说过“*如果你所要做的就是按照最近的日期排序,那么这个工作就会完成*”。 – James

+0

来自OP:**“我需要对此列表进行排序,以便与DateTime.Now最接近的对象首先在列表中。”**。此外,您在我的第一条评论后添加了该部分。 – Rotem

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

如何回合

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