按日期排序文件,但按名称排序文件夹
本文关键字:排序 文件夹 日期 文件 | 更新日期: 2023-09-27 18:01:51
试图在顶部显示按名称排序的文件夹,然后按日期排序的下面的文件,我已经制作了这个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);
}
}
}
结果是,我得到的文件夹首先列出,但只有一些按日期排序。我对iccomparer的逻辑正确吗?
当你比较一个文件和一个文件夹时,它们不应该被认为是等价的,而应该把文件夹放在第一位。