C#按DateTime对List <arraylist>中的项目进行排序

时间:2018-02-12 03:32:03

标签: c# list arraylist

我有一个arraylist List<ArrayList> hallDetailsList的列表,我正在尝试按时间排序

列出数据

hallDetailsList:
[0][0]Movie A|4:10pm|Hall 1
[0][1]Movie A |6:50pm|Hall 1
[0][2]Movie A |9:30pm|Hall 1
[0][3]Movie A |12:10am|Hall 1
[1][0]Movie B|4:40pm|Hall 2
[1][1]Movie B|9:40pm|Hall 2
[1][2]Movie B|12:10am|Hall 2
[1][3]Movie B|7:10pm|Hall 2
[1][4]Movie B|12:00pm|Hall 2
[1][5]Movie B|2:40pm|Hall 2
[1][6]Movie B|8:30pm|Hall 2
[2][0]Movie B|1:00pm|Hall 3
[2][1]Movie C|3:10pm|Hall 3
[2][2]Movie C|9:20pm|Hall 3
[2][3]Movie C|11:30pm|Hall 3
[2][4]Movie C|5:20pm|Hall 3
[2][5]Movie C|7:20pm|Hall 3
[3][0]Movie C|1:00pm|Hall 4
[3][1]Movie C|3:00pm|Hall 4
[3][2]Movie D|3:40pm|Hall 4
[3][3]Movie D|6:30pm|Hall 4
[3][4]Movie D|9:20pm|Hall 4
[3][5]Movie D|12:15am|Hall 4
[3][6]Movie D|5:30pm|Hall 4
[3][7]Movie D|12:30pm|Hall 4

我设法通过大厅对数据进行排序,因为之前它们只是乱七八糟。我如何通过时间对物品进行排序?就像现在在一些arraylist,时间搞砸了

1 个答案:

答案 0 :(得分:1)

鉴于此

List<ArrayList> hallDetailsList = new List<ArrayList>()
   {
      new ArrayList()
         {
            0,
            0,
            "Movie A",
            "1:10pm",
            "Hall 1"
         },
      new ArrayList()
         {
            0,
            0,
            "Movie A",
            "4:10pm",
            "Hall 1"
         },
      new ArrayList()
         {
            0,
            0,
            "Movie A",
            "3:10pm",
            "Hall 1"
         }

   };

您可以这样订购

var results = hallDetailsList.OrderBy(item => DateTime.ParseExact(((string)item[3]), "h:mmtt", null, System.Globalization.DateTimeStyles.None))
                             .ToList();

或稍微更具可读性

var Result= hallDetailsList.OrderBy(item =>
              {
                  var time = (string)item[3];
                  return DateTime.ParseExact(time, "h:mmtt", null, System.Globalization.DateTimeStyles.None);
              }).ToList();

注意:根据您的实际ArrayList,您可能需要更改

上的索引
var time = (string)item[3];