首页 新闻 会员 周边 捐助

请问两个list数组的比较以及排序

0
悬赏园豆:30 [已解决问题] 解决于 2015-10-29 09:15

List<dynamic> list1 = new List<dynamic>();
list1.Add(new { id = "1", num = 11, time = "2015-10-10 10:10:10" });
list1.Add(new { id = "2", num = 22, time = "2015-10-10 11:11:11" });
list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" });

List<dynamic> list2 = new List<dynamic>();
list2.Add(new { id = "11", num = 18, time = "2015-10-10 10:10:10" });
list2.Add(new { id = "21", num = 10, time = "2015-10-10 11:11:11" });
list2.Add(new { id = "3", num = 55, time = "2015-10-10 12:12:12" });

 

有上面2个数组,以两个数组的时间相等,判断出2个num值偏差比较大的那条数据,如上列2列要返回 list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" });

菜菜灰的主页 菜菜灰 | 初学一级 | 园豆:88
提问于:2015-10-28 15:55
< >
分享
最佳答案
0
static void Main(string[] args)
{
      List<dynamic> list1 = new List<dynamic>();
      list1.Add(new { id = "1", num = 11, time = "2015-10-10 10:10:10" });
      list1.Add(new { id = "2", num = 22, time = "2015-10-10 11:11:11" });
      list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" });
      List<dynamic> list2 = new List<dynamic>();
      list2.Add(new { id = "11", num = 18, time = "2015-10-10 10:10:10" });
      list2.Add(new { id = "21", num = 10, time = "2015-10-10 11:11:11" });
      list2.Add(new { id = "31", num = 55, time = "2015-10-10 12:12:12" });

      var maxId = "";
      var maxAbs = 0;
      list1.ForEach(p =>
      {
          var numList = list2.FindAll(c => c.time == p.time).Select(c=>c.num);
          var maxNum = numList.Max();
          var minNum = numList.Min();

          if (Math.Abs(p.num - maxNum) > Math.Abs(p.num - minNum))
          {
              if (maxAbs < Math.Abs(p.num - maxNum))
             {
                 maxAbs = Math.Abs(p.num - maxNum);
                 maxId = p.id;
               }
      }
      else
      {
        if (maxAbs < Math.Abs(p.num - minNum))
        {
          maxAbs = Math.Abs(p.num - minNum);
          maxId = p.id;
        }
      }
    });

    var max= list1.Find(p => p.id == maxId);
    Console.WriteLine("结果是 id={0},num={1},time={2}", max.id, max.num, max.time);
    Console.ReadKey();
}

这样好像能达到你的要求. 没有仔细的测试过..仅供参考..

收获园豆:20
李丶GuanYao | 小虾三级 |园豆:1228 | 2015-10-28 17:14
其他回答(1)
0

可以通过linq表达式查询。

收获园豆:10
waiter | 园豆:1000 (小虾三级) | 2015-10-28 16:38

具体点的代码呢~

支持(0) 反对(0) 菜菜灰 | 园豆:88 (初学一级) | 2015-10-28 16:51

@菜菜灰: 没看懂题目要求。。

参考下: http://bbs.csdn.net/topics/390233316 

支持(0) 反对(0) waiter | 园豆:1000 (小虾三级) | 2015-10-28 16:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册