首页 > 解决方案 > 排序列表 C# 时的一些神秘行为

问题描述

我需要对我的List<FileObj>. 我需要根据 FileObj 作为路径包含的日期来做

fileObj.fullPath = "C:\\Downloads\\2020_11_30_12_34\\myfilename.txt"

所以,我需要从这个路径中对数据进行子串转换并比较它们。为了做到这一点,我使用了这样的方法

filteredFiles.Sort((x, y) = >
{
    string firstPath = x.FullPath;
    string secondPath = y.FullPath;

    List<string> splitFirstPath = firstPath.Split(DELIMITER).ToList();
    List<string> splitSecondPath = secondPath.Split(DELIMITER).ToList();

    DateTime ? firstPathDate = null;

    foreach(string tmp in splitFirstPath)
    {
        firstPathDate = DateUtil.ConvertStringToDate(tmp, Constants.DATE_PATTERN);

        if (firstPathDate != null)
        {
            break;
        }
    }

    DateTime ? secondPathDate = null;

    foreach(string tmp in splitSecondPath)
    {
        secondPathDate = DateUtil.ConvertStringToDate(tmp, Constants.DATE_PATTERN);

        if (secondPathDate != null)
        {
            break;
        }
    }

    int result = (firstPathDate != null
        && secondPathDate != null
        && firstPathDate.Value != null
        && secondPathDate.Value != null) ? secondPathDate.Value.CompareTo(firstPathDate.Value) : -1;

    return result;
});

DELIMITER 在这里,\\所以我只是尝试转换路径的每个部分,如果成功,我会得到一个日期,如果不是,我会得到 null,然后我在最后比较它们两个。

问题是,除了上述预期路径 - “C:\Downloads\2020_11_30_12_34\myfilename.txt”,还有许多其他路径甚至不包含日期,例如“C:\Downloads\examplefolder \我的文件名.txt"

实际上这应该不是问题,因为如果我无法转换日期,我会得到 null 并比较结果是否-1意味着这些文件将位于列表的末尾,对吗?

但是神秘的是 - 如果我在我的原始列表中有 10 个这样的“错误”路径,它会按预期工作并且它们都到了列表的末尾,但是如果我有 20 个这样的“错误”路径,这个方法给出我在这里Sort((x, y)x - 作为 null ref obj。

怎么可能?我确切地知道这个列表filteredFiles不包含任何空对象我在调试时看到它我看到输入的所有对象都没有任何空值。

在第 20 次迭代中我从哪里得到这个神秘的空对象?

PS我知道我可以检查null,但我很感兴趣它是如何出现在列表中的?

编辑

最终我以这种方式修复了它

        try
        {
            contentFromAllAgents.Sort((x, y) =>
            {
                string firstPath = x?.FullPath;
                string secondPath = y?.FullPath;

                List<string> splitFirstPath = firstPath?.Split(DELIMITER)?.ToList();
                List<string> splitSecondPath = secondPath?.Split(DELIMITER)?.ToList();

                DateTime? firstPathDate = null;

                foreach (string tmp in splitFirstPath ?? Enumerable.Empty<string>())
                {
                    firstPathDate = DateUtil.ConvertStringToDate(tmp, Constants.DATE_PATTERN);

                    if (firstPathDate != null)
                    {
                        break;
                    }
                }

                DateTime? secondPathDate = null;

                foreach (string tmp in splitSecondPath ?? Enumerable.Empty<string>())
                {
                    secondPathDate = DateUtil.ConvertStringToDate(tmp, Constants.DATE_PATTERN);

                    if (secondPathDate != null)
                    {
                        break;
                    }
                }

                if (firstPathDate == null)
                {
                    firstPathDate = DateTime.MinValue;
                }

                if (secondPathDate == null)
                {
                    secondPathDate = DateTime.MinValue;
                }

                int result = (firstPathDate != null
                            && secondPathDate != null
                            && firstPathDate.Value != null
                            && secondPathDate.Value != null) ? secondPathDate.Value.CompareTo(firstPathDate.Value) : -1;

                return result;
            });
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error while sorting :: {e}");
        }

我已经为那些为 null 的人提供了默认值,另外还添加try/catch了以防万一我有任何其他错误,因为在我的情况下,在排序中发现这个问题并不容易,因为它没有任何错误(我不不知道为什么)只是卡住了。

标签: c#

解决方案


推荐阅读