Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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#_Asp.net_Sorting - Fatal编程技术网

C# 用数值对类字符串字段属性排序

C# 用数值对类字符串字段属性排序,c#,asp.net,sorting,C#,Asp.net,Sorting,我有一个类,它有一个属性“Name”,其中包含像“1[AnnualRevenue]、“2[ResellerType]、“3\uXXX”… 我的课就像 class xxx { private string fileName; public string FileName { get { return fileName; } set { fileName = value; } } } 我给类的对象赋值。比如xxx.

我有一个类,它有一个属性“Name”,其中包含像
“1[AnnualRevenue]、“2[ResellerType]、“3\uXXX”…

我的课就像

class xxx
{
 private string fileName;
 public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }
}
我给类的对象赋值。比如
xxx.FileName=“1_u2;[年度收入]”

现在我有一个列表类。现在根据这个类属性对列表进行排序

现在我想根据数字顺序对字段进行排序,我的意思是1前2秒,依此类推

然后将其写入filestream

有人能帮我吗。
提前谢谢

您可以使用LINQ为您做这件事

List<xxx> orderedList = unOrderedList.OrderBy(o => Convert.ToInt32(o.FileName.Split('_').First())).ToList();
List orderedList=unOrderedList.OrderBy(o=>Convert.ToInt32(o.FileName.Split(“”“).First()).ToList();

代表评论编辑了答案-指出我们确实需要转换为整数才能正确排序。

您可以使用LINQ为您做这件事

List<xxx> orderedList = unOrderedList.OrderBy(o => Convert.ToInt32(o.FileName.Split('_').First())).ToList();
List orderedList=unOrderedList.OrderBy(o=>Convert.ToInt32(o.FileName.Split(“”“).First()).ToList();

代表评论编辑了答案-指出我们确实需要转换为整数才能正确排序。

您可以按照以下方式对列表进行排序:

List<xxx> list = new List<xxx> 
{ 
    new xxx { FileName = "3_a" }, 
    new xxx { FileName = "1_a" }, 
    new xxx { FileName = "2_a" }, 
    new xxx { FileName = "8_a" } 
};
var sorted = list.OrderBy(it => Convert.ToInt32(it.FileName.Split('_')[0]));//using System.Linq;

您可以执行以下操作对列表进行排序:

List<xxx> list = new List<xxx> 
{ 
    new xxx { FileName = "3_a" }, 
    new xxx { FileName = "1_a" }, 
    new xxx { FileName = "2_a" }, 
    new xxx { FileName = "8_a" } 
};
var sorted = list.OrderBy(it => Convert.ToInt32(it.FileName.Split('_')[0]));//using System.Linq;

由于属性是一个
字符串
,但您希望对其进行数字排序,因此最好的方法可能是在类上实现
IComparable
,然后将自定义排序代码放入
CompareTo
方法中。这样,每次要对列表进行排序时就不必编写更复杂的Lambda语句,只需调用列表上的
Sort()
方法即可

您还可以处理
FileName
属性不包含下划线或
null
的情况,而不是在
OrderBy
代码中出现异常(这是大多数其他答案都会出现的情况)

我还做了一些其他更改-覆盖
ToString
方法,以便您可以轻松地将值显示到控制台窗口,并对
FileName
属性使用自动属性语法,以便我们可以删除支持字段:

class xxx : IComparable<xxx>
{
    public string FileName { get; set; }

    public int CompareTo(xxx other)
    {
        // Short circuit if any object is null, if the
        // Filenames equal each other, or they're empty
        if (other == null) return 1;
        if (FileName == null) return (other.FileName == null) ? 0 : -1;
        if (other.FileName == null) return 1;
        if (FileName.Equals(other.FileName)) return 0;
        if (string.IsNullOrWhiteSpace(FileName)) 
            return (string.IsNullOrWhiteSpace(other.FileName)) ? 0 : -1; 
        if (string.IsNullOrWhiteSpace(other.FileName)) return 1;

        // Next, try to get the numeric portion of the string to compare
        int thisIndex;
        int otherIndex;

        var thisSuccess = int.TryParse(FileName.Split('_')[0], out thisIndex);
        var otherSuccess = int.TryParse(other.FileName.Split('_')[0], out otherIndex);

        // If we couldn't get the numeric portion of the string, use int.MaxValue
        if (!thisSuccess)
        {
            // If neither has a numeric portion, just use default string comparison
            if (!otherSuccess) return FileName.CompareTo(other.FileName);
            thisIndex = int.MaxValue;
        }
        if (!otherSuccess) otherIndex = int.MaxValue;

        // Return the comparison of the numeric portion of the two filenames
        return thisIndex.CompareTo(otherIndex);
    }

    public override string ToString()
    {
        return FileName;
    }
}

由于属性是一个
字符串
,但您希望对其进行数字排序,因此最好的方法可能是在类上实现
IComparable
,然后将自定义排序代码放入
CompareTo
方法中。这样,每次要对列表进行排序时就不必编写更复杂的Lambda语句,只需调用列表上的
Sort()
方法即可

您还可以处理
FileName
属性不包含下划线或
null
的情况,而不是在
OrderBy
代码中出现异常(这是大多数其他答案都会出现的情况)

我还做了一些其他更改-覆盖
ToString
方法,以便您可以轻松地将值显示到控制台窗口,并对
FileName
属性使用自动属性语法,以便我们可以删除支持字段:

class xxx : IComparable<xxx>
{
    public string FileName { get; set; }

    public int CompareTo(xxx other)
    {
        // Short circuit if any object is null, if the
        // Filenames equal each other, or they're empty
        if (other == null) return 1;
        if (FileName == null) return (other.FileName == null) ? 0 : -1;
        if (other.FileName == null) return 1;
        if (FileName.Equals(other.FileName)) return 0;
        if (string.IsNullOrWhiteSpace(FileName)) 
            return (string.IsNullOrWhiteSpace(other.FileName)) ? 0 : -1; 
        if (string.IsNullOrWhiteSpace(other.FileName)) return 1;

        // Next, try to get the numeric portion of the string to compare
        int thisIndex;
        int otherIndex;

        var thisSuccess = int.TryParse(FileName.Split('_')[0], out thisIndex);
        var otherSuccess = int.TryParse(other.FileName.Split('_')[0], out otherIndex);

        // If we couldn't get the numeric portion of the string, use int.MaxValue
        if (!thisSuccess)
        {
            // If neither has a numeric portion, just use default string comparison
            if (!otherSuccess) return FileName.CompareTo(other.FileName);
            thisIndex = int.MaxValue;
        }
        if (!otherSuccess) otherIndex = int.MaxValue;

        // Return the comparison of the numeric portion of the two filenames
        return thisIndex.CompareTo(otherIndex);
    }

    public override string ToString()
    {
        return FileName;
    }
}


你的意思是你有一个类的列表,你想按它们的名称属性排序吗?如果您展示了该类的代码示例以及如何存储该类的实例,这将非常有帮助。请展示您的类以及值的示例您正在谈论文件名开头的“N_uu”号吗?这可以让你开始,如果你正在考虑,那么你没有做数字排序,那就是字母数字排序,这将使10_a比2_a提前你的意思是你有一个类列表,你想按它们的名称属性排序?如果您展示了该类的代码示例以及如何存储该类的实例,这将非常有帮助。请展示您的类以及值的示例您正在谈论文件名开头的“N_uu”号吗?这可以让你开始,如果你正在考虑,那么你没有做数字排序,这是字母数字排序,它会把10 a放在2 a之前,这会把10 a,20 a放在这里!可能不需要对文件名调用
.ToString()
:这里会放10个,20个!可能不需要对文件名调用
.ToString()
:这里会放10μa,20μa,解和上面一样虽然我需要尝试,但这听起来很好,纯数字排序它会放在哪里,10μa,20μa,解和上面一样虽然我需要尝试,但这听起来很好,纯数字排序顺便说一句,不确定你是怎么得到文件名的,但是,如果您在代码中的某个地方向它们添加数字,那么给类一个
intrank{get;set;}
属性并在文件名之外设置它可能会更容易。如果您这样做了,那么您就可以这样做:
list.OrderBy(i=>i.Rank)
,并且您不必创建自定义的CompareTo方法。顺便说一下,不确定如何生成文件名,但是如果您在代码中的某个地方向它们添加数字,那么给类一个
int-Rank{get;set;}可能会更容易一些
属性,并在文件名之外设置该属性。如果您这样做了,那么您可以只做:
list.OrderBy(i=>i.Rank)
,而不必创建自定义的CompareTo方法。