本文介绍了如何按asc顺序对字符串列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表模型

公共类KitModel

{

public string KitName {get;组; }

公共字符串KitCode {get;组; } $ / $
公共小数KitValue {get;组; }

}

List< kitmodel> listKitModel = new List< kitmodel>();

有这样的数据

listKitModel [0] .KitCode =BIN

listKitModel [ 1] .KitCode =CAN9

listKitModel [0] .KitCode =CAN8

listKitModel [0] .KitCode =CAN10

listKitModel [0] .KitCode =CAN11

listKitModel [0] .KitCode =CAN12



什么我试过了:



我喜欢这样

listKitModel = listKitModel。 OrderBy(x => x.KitCode)。ToList();



我这样出来

listKitModel [0] .KitCode =BIN

listKitModel [0] .KitCode =CAN10

listKitModel [0] .KitCode =CAN11

listKitModel [0] .KitCode =CAN12

listKitModel [1] .KitCode =CAN8

listKitModel [0]。 KitCode =CAN9

但预期输出为asc

listKitModel [0] .KitCode =BIN

listKitModel [0] .KitCode =CAN8

listKitModel [1] .KitCode =CAN9

listKitModel [0] .KitCode =CAN10

listKitModel [0] .KitCode =CAN11

listKitModel [0] .KitCode =CAN12

I have a list model
public class KitModel
{
public string KitName { get; set; }
public string KitCode { get; set; }
public decimal KitValue { get; set; }
}
List<kitmodel> listKitModel=new List<kitmodel>();
having data like this
listKitModel[0].KitCode ="BIN"
listKitModel[1].KitCode ="CAN9"
listKitModel[0].KitCode ="CAN8"
listKitModel[0].KitCode ="CAN10"
listKitModel[0].KitCode ="CAN11"
listKitModel[0].KitCode ="CAN12"

What I have tried:

i sort like this
listKitModel= listKitModel.OrderBy(x => x.KitCode).ToList();

Am getting out like this
listKitModel[0].KitCode ="BIN"
listKitModel[0].KitCode ="CAN10"
listKitModel[0].KitCode ="CAN11"
listKitModel[0].KitCode ="CAN12"
listKitModel[1].KitCode ="CAN8"
listKitModel[0].KitCode ="CAN9"
But Expected output is asc
listKitModel[0].KitCode ="BIN"
listKitModel[0].KitCode ="CAN8"
listKitModel[1].KitCode ="CAN9"
listKitModel[0].KitCode ="CAN10"
listKitModel[0].KitCode ="CAN11"
listKitModel[0].KitCode ="CAN12"

推荐答案

Func<string, string> mySort = ( s ) => {
   string alphas = s.PadRight( 3 ).Substring( 0, 3 );
   string digitChars = s.PadRight( 5, ' ' ).Substring( 3, 2 ).Trim();
   int digits = string.IsNullOrEmpty( digitChars ) ? 0 : int.Parse( digitChars );

   return



public class KitModel
{
    public string KitName { get; set; }
    public string KitCode { get; set; }
    public decimal KitValue { get; set; }

    public string KitCodePrefix { get; set; }
    public int KitIndex { get; set; }
}







List<KitModel> lst = new List<KitModel>();

lst.Add(new KitModel()
{
    KitCode = "BIN",
    KitCodePrefix = "BIN",
    KitIndex = 0
});
lst.Add(new KitModel()
{
    KitCode = "CAN8",
    KitCodePrefix = "CAN",
    KitIndex = 8
});
lst.Add(new KitModel()
{
    KitCode = "CAN9",
    KitCodePrefix = "CAN",
    KitIndex = 9
});
lst.Add(new KitModel()
{
    KitCode = "CAN12",
    KitCodePrefix = "CAN",
    KitIndex = 12
});

lst.Sort(delegate (KitModel x, KitModel y)
{
    int a = x.KitCodePrefix.CompareTo(y.KitCodePrefix);
    if (a == 0)
        a = x.KitIndex.CompareTo(y.KitIndex);
    return a;
});


这篇关于如何按asc顺序对字符串列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 19:16