对列表进行排序< String>包括按日期的日期值 [英] Sort a List<String> that includes date values by date

查看:38
本文介绍了对列表进行排序< String>包括按日期的日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个时钟程序,但是我的时间安排在正确的顺序时遇到了问题.日期是有序的,但是list.Sort()使时间混乱了.它像一个字符串一样对它进行排序,这很有意义,因为它是一个字符串.3:41 PM在7:20 AM之前进行排序,因为3在7之前.请参见示例:

I am working on a time clock program and I am having an issue getting my time punches to be in the correct order. The dates are in order, but the list.Sort() is putting the times out of order. It is sorting it like a string, which make sense because it is a string. 3:41PM is sorted before 7:20AM because 3 is before 7. See example:

 12/17/2018 3:41:00 PM         Clock Out         Yes            BB  
 12/17/2018 7:20:00 AM         Clock In          NO             Not Needed

由于要转储到列表中的信息,我不确定如何完成此操作.

I am not sure how to accomplish this because of the information I am dumping into the list.

while (reader.Read())
{
     timeClockDataList.Add(reader["Punch"].ToString() + "%" + reader["PunchType"].ToString() + "%" + reader["NeedsApproval"].ToString() + "%" + reader["Approval"].ToString());
}

我将%"放入其中,以便稍后可以在%处分割字符串,以在打卡时间中插入打卡时间,打卡类型,所需的批准和批准.

I am putting the "%" in there so I can split the string at the % later to populate the time card with the punch time, type of punch, approval needed, and approval.

我的问题是如何按日期和时间对字符串进行排序?

My question is how to sort this string by the Date AND Time?

EDIT

EDIT

while (reader.Read())
{
    timeClockDataList.Add(new ClockData
    {
     Punch = DateTime.Parse(reader["Punch"].ToString()),
     PunchType = reader["PunchType"].ToString(),
     NeedsApproval = reader["NeedsApproval"].ToString(),
     Approval = reader["Approval"].ToString(),
   });

//***This is the old code that makes one long string***
//timeClockDataList.Add(reader["Punch"].ToString() + "%" + ToString() + +                      
}

timeClockDataList.OrderBy(x => x.Punch);
//***This is the old code that would sort the list string***
//timeClockDataList.Sort();    

using (StreamWriter writer = new StreamWriter(filePath, true))
{
    for (int _i = 0; _i < timeClockDataList.Count; ++_i)
    {
         punch = timeClockDataList[_i].Punch.ToString();
         punchType = timeClockDataList[_i].PunchType;
         needsApproval = timeClockDataList[_i].NeedsApproval;
         approval = timeClockDataList[_i].Approval;

         writer.WriteLine(String.Format("{0,-5}{1,-30}{2,-20}{3,-11}{4,-15}", "     ", punch, punchType, needsApproval, approval));

            punch = null;
            punchType = null;
            needsApproval = null;
            approval = null;
   }
  }

推荐答案

timeClockDataList 是错误的类型.如果所有内容都是一个大字符串,那么您就没有数据,而只有一个大字符串.

timeClockDataList is the wrong type. When everything is one big string then you don't have data, you just have one big string.

创建一个自定义对象来存储您的数据.例如:

Make a custom object to store your data. For example:

class ClockData
{
    public DateTime Punch { get; set; }
    public string PunchType { get; set; }
    // etc.
}

将数据读取到该类的列表中:

Read your data into a list of that class:

while (reader.Read())
{
    timeClockDataList.Add(new ClockData
    {
        Punch = DateTime.Parse(reader["Punch"].ToString()),
        PunchType = reader["PunchType"].ToString(),
        // etc.
    });
}

现在您有了实际的数据,可以对它们进行操作/排序/其他操作.轻松地:

Now you have actual data, which can be manipulated/sorted/etc. easily:

timeClockDataList.OrderBy(x => x.Punch)

您可能还希望在填充它时进行一些错误检查,对 DateTime 使用 TryParseExact ,等等.您可以进行多种改进.最终,当您要显示数据时,即为,当您将其输出为字符串时.(您可以通过在自定义类上覆盖 .ToString()来使其非常简单.)

You may also want to throw in some error checking when populating it, use TryParseExact for the DateTime, etc. There are a variety of improvements you can make. Eventually, when you want to display the data, that's when you output it as a string. (You can make that very simple by overriding .ToString() on the custom class.)

这篇关于对列表进行排序&lt; String&gt;包括按日期的日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆