按日期排序文件,但按名称排序文件夹

Sort files by date but folders by name

为了尝试在顶部显示按名称排序的文件夹,然后在下方显示按日期排序的文件,我制作了这个 PropertyGroupDescription 和 IComparer

using (ItemCollectionView.DeferRefresh())
{

    var dataView = (ListCollectionView)CollectionViewSource.GetDefaultView(_fileCollection);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("ObjectType");
    dataView.GroupDescriptions.Add(groupDescription);
    dataView.CustomSort = new StringComparerFiles(false);  
}


public class StringComparerFiles : IComparer 
{

    public StringComparerFiles() : this(false) { }
    public StringComparerFiles(bool descending) { }
    //descending not implemented yet
    public int Compare(object a, object b)
    {
        bool xFolder = false;
        bool yFolder = false;
        string xName = string.Empty;
        string yName = string.Empty;
        DateTime xDate = new DateTime();
        DateTime yDate = new DateTime();


        if (a is FileData)
        {
            xDate = (a as FileData).FileDate; 
        }
        else
        {
            xFolder = true;
            xName = (a as FolderData).FolderName;
        }

        if (b is FileData)
        {
            yDate = (b as FileData).FileDate; 
        }
        else
        {
            yFolder = true;
            yName = (b as FolderData).FolderName;
        }


        if (xFolder && yFolder)
        {
            int n = SafeNativeMethods.StrCmpLogicalW(xName, yName);
            return n;
        }
        else if (xFolder || yFolder) 
            return 0;  //don't compare file and folder
        else
        {
            return DateTime.Compare(xDate, yDate);
        }              
    }
}

结果是我首先列出了文件夹,但只有一些文件夹按日期排序。我的 IComparer 逻辑正确吗?

当您将文件与文件夹进行比较时,不应将它们视为等同的,但文件夹应该放在第一位。