gpt4 book ai didi

c# - 如何对包含文件大小数据的 ListView 列进行排序? C#

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:49 24 4
gpt4 key购买 nike

我想对 ListView 的列内的项目进行排序,我已经做到了,但是......我不能用列中的数据类型(见图),有人知道怎么做吗?

Type Data example:

最佳答案

为排序函数编写自定义比较器,如下所示:

    /// <summary>
/// Comparator for values like 123 KB
/// </summary>
/// <param name="x">First value to compare</param>
/// <param name="y">Second value to compare</param>
/// <returns>0 for equal, 1 for x &gt; y, -1 for x &lt; y</returns>
int Compare(object x, object y)
{
// Convert to strings
string strX = null;
if (x is string)
strX = (string)x;
else if (x != null)
strX = x.ToString();
string strY = null;
if (y is string)
strY = (string)y;
else if (y != null)
strY = y.ToString();

// Nulls first (null means less, since it's blank)
if (strX == null)
{
if (strY == null)
return 0;
return -1;
}
else if (strY == null)
return 1;

// Convert the non-KB part to a number
double numX;
double numY;
if (strX.EndsWith("KB") || strX.EndsWith("GB") || strX.EndsWith("MB"))
strX = strX.Substring(0, strX.Length - 2);
if (strX.EndsWith("Bytes"))
strX = strX.Substring(0, strX.Length - 5);
strX = strX.Trim();
double.TryParse(strX, out numX);
if (strY.EndsWith("KB") || strY.EndsWith("GB") || strY.EndsWith("MB"))
strY = strY.Substring(0, strY.Length - 2);
if (strY.EndsWith("Bytes"))
strY = strX.Substring(0, strY.Length - 5);
strY = strY.Trim();
double.TryParse(strY, out numY);

// Compare the numbers
return numX.CompareTo(numY);
}

关于c# - 如何对包含文件大小数据的 ListView 列进行排序? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com