泛型 List中的Sort三个方法(单个字段排序+多个字段排序)

常用的两种使用scort()进行排序的方法
对于List按照某一个字段的值进行排序,使用系统提供的sort方法进行排序,需要继承ICompare接口实现参数中的CompareTo方法,注意参数一定是Object。CompareTo方法只能进行两个数据的比较,但是系统可以实现对整个list中的数据的排序。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
List<Student> stuList = new List<Student>();
            stuList.Add(s1);
            stuList.Add(s2);
            stuList.Add(s3);
            stuList.Add(s4);
            stuList.Add(s5);
            stuList.Add(s6);
 
            for (int i = 0; i < stuList.Count; i++)
            {
                Console.WriteLine(stuList[i]);
            }
            Console.WriteLine("------------------------");
            stuList.Sort();
            for (int i = 0; i < stuList.Count; i++)
            {
                Console.WriteLine(stuList[i]);
            }
public int CompareTo(object other)
        {
            return this.Age - ((Student)other).Age;
        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ps:可以通过改变this.Age和other.Age的顺序控制是升序还是降序,方法的返回值必须和接口中的方法保持一致

参考多个字段进行排序
自己实现方法,方法的返回值必须是int,在sort中使用lambda表达式实现排序
可以通过设置权重来控制字段的优先级

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 List<Student> stuList = new List<Student>();
            stuList.Add(s1);
            stuList.Add(s2);
            stuList.Add(s3);
            stuList.Add(s4);
            stuList.Add(s5);
 
        多个方法之间要使用 + 号
            stuList.Sort(((x, y) => x.sortByAge(y) * 4 + x.sortByChinese(y)));//系统规定必须是int
 
            for (int i = 0; i < stuList.Count; i++)
            {
                Console.WriteLine(stuList[i]);
            }
 
    public int sortByAge(Student s)
        {
            int result = this.Age - s.Age;
            return result==0?0:(result>0?1:-1);
        }
        public int sortByChinese(Student s)
        {
            int result= (int)(s.Chinese-this.Chinese);//变量的位置确定了正序还是倒序
            return result == 0 ? 0 : (result > 0 ? 1 : -1);
        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--------------------- 
作者:高冷技术宅 
来源:CSDN 
原文:https://blog.csdn.net/qq_38280936/article/details/77776495?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

下面附上自己写的:自定义的数据类TreeViewData

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <summary>
/// 树形菜单数据
/// </summary>
public class TreeViewData
{
    /// <summary>
    /// 数据内容
    /// </summary>
    public string Name;
    /// <summary>
    /// 数据所属的父ID
    /// </summary>
    public int ParentID;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 void Awake()
    {
        List<TreeViewData> datas = new List<TreeViewData>();

        TreeViewData data = new TreeViewData();

       for (int i = 0; i < length; i++)//多次填充,依据parentID(主)和Name(次)排序
        {
            data = new TreeViewData();
            data.Name = "1.1.1.2.1.第一题";
            data.ParentID = 8;
            datas.Add(data);
        }
        datas.Sort((x, y) => sortByTreeViewDataParentID(x,y)*4+sorByTreeViewDataName(x,y));//系统规定必须是int

 }


    public int sortByTreeViewDataParentID(TreeViewData x,TreeViewData y)
    {
        int result = x.ParentID - y.ParentID;
        return result == 0 ? 0 : (result > 0 ? 1 : -1);
    }
    public int sorByTreeViewDataName(TreeViewData x, TreeViewData y) {
        CultureInfo PronoCi = new CultureInfo(2052);
        int result = string.Compare(y.Name, x.Name, true,PronoCi);
        return result == 0 ? 0 : (result>0?1:-1);
    }

以上可以起到对List排序效果;

上述的lamda表达式还可以修改成如下:(默认是汉字拼音的读音顺序)

 CultureInfo PronoCi = new CultureInfo(2052);

 datas.Sort((x, y) =>
                    {
                        int result;
                        if (x.building_name == y.building_name)
                        {
                            result = 0;
                        }
                        else
                        {
                            if (string.Compare(x.building_name, y.building_name, true, PronoCi) > 0)
                            {
                                result = 1;
                            }
                            else
                            {
                                result = -1;
                            }
                        }
                        return result;
                    });

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyDog阿祥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值