Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 排序错误-统一日期_C#_List_Sorting_Unity3d - Fatal编程技术网

C# 排序错误-统一日期

C# 排序错误-统一日期,c#,list,sorting,unity3d,C#,List,Sorting,Unity3d,这是一个有趣的例子。我正在使用iComparable界面按日期对列表进行排序。今天我又在测试,突然发现了一些东西。我已进入2016年11月1日作为日期。当数据发送到屏幕时,该日期是第一个日期,即使列表中的其他日期早于该日期。我意识到系统会认为11可能需要排在列表的首位,但我如何才能解决这个问题 public class SortbyDate :IComparer<OilChange> { public int Compare(OilChange x, OilChange y) {

这是一个有趣的例子。我正在使用iComparable界面按日期对列表进行排序。今天我又在测试,突然发现了一些东西。我已进入2016年11月1日作为日期。当数据发送到屏幕时,该日期是第一个日期,即使列表中的其他日期早于该日期。我意识到系统会认为11可能需要排在列表的首位,但我如何才能解决这个问题

public class SortbyDate :IComparer<OilChange> {
  public int Compare(OilChange x, OilChange y) {
    return x.ServiceDate.CompareTo (y.ServiceDate);
  }
}
公共类排序日期:IComparer{
公共整数比较(换油x,换油y){
返回x.ServiceDate.CompareTo(y.ServiceDate);
}
}

谢谢

如果你把它作为一个字符串来排序,11将出现在2之前,例如,使11月出现在2月之前。如果它表示的值是一个日期,则可能应该将其存储并作为日期进行操作

以下是我用于日期交换的函数

public string SwitchDate(string date)
{
    Debug.Log ("Date IN: Switch " + date);
    string[] inputSections=date.Split('/');
    string ServiceDate=string.Format("{0}/{1}/{2}", inputSections[2], inputSections[0], inputSections[1]); 
    Debug.Log ("Date OUT: Switch " + ServiceDate);

    return ServiceDate;
}

string SwitchbackDate(string date)
{
    Debug.Log ("Switchback In: " +date);
    string[] inputSections=date.Split('/');
    string ServiceDate=string.Format("{0}/{1}/{2}",inputSections[1], inputSections[2], inputSections[0]); 
    Debug.Log ("Switchback Out: " + ServiceDate);

    return ServiceDate;
}

我确实建议将字符串变量的类型更改为DateTime。操纵日期是更好的方法。但是,如果出于某种原因希望坚持使用字符串,可以在排序时将其转换为日期

public class SortbyDate :IComparer<OilChange>
{
    public int Compare(OilChange x, OilChange y)
    {
        DateTime dateX = Convert.ToDateTime(x.ServiceDate);
        DateTime dateY = Convert.ToDateTime(y.ServiceDate);
        return dateX.CompareTo(dateY);
    }
}

阅读更多。

奇怪。“OilChange.ServiceDate”属性的类型是什么?它真的是一个日期时间还是一个字符串?它是一个字符串。DateTime有太多问题。这就是排序问题的原因。DateTime有什么问题?我无法消除时间部分。删除时间、冒号等无关信息的最佳方法是什么?当您输出日期(以网格、标签或其他形式)时,
ToString()
使用日期格式。例如
myDate.ToString(“dd/MMM/yyyy”)
请参阅:了解更多详细信息!我返回并在一些旧数据中添加了11/1/xxxx,不幸的是,它再次将日期推到顶部。有什么想法吗?你是如何将xxxx年11月1日转换成datetime对象的?如果您使用的是
DateTime.Parse
,它可能会颠倒月份和日期。最好是
DateTime.ParseExact
使用日期时间格式来强制执行月/日订单感谢Guillerme!成功了!也谢谢乔纳森!
string date = String.Format("{0:MM/dd/yyyy}", dateTimeValue);