您当前的位置: 首页 >  c#

幻世界

暂无认证

  • 0浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【c#编程技术总结】c#List排序补充

幻世界 发布时间:2019-04-23 17:02:25 ,浏览量:0

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

 //方法一sort排序使用lambda表达式

 List list = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            list.Sort((x, y) => -x.CompareTo(y));//降序
            list.Sort((x, y) => x.CompareTo(y));//升序

  //方法二简单sort排序

   List list = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            list.Reverse();// 反转顺序          
            list.Sort();// 升序排序

 //方法三复杂对象

   List list = new List();
            list.Sort(
                delegate (Student p1, Student p2)
                 {
                     return p1.sno.CompareTo(p2.sno);//升序
                     //return p1.sno == p1.sno ? 0 : (p1.sno > p1.sno) ? 1 : -1;
                 });
            //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });

 方法四OrdeOrderBy运用

Debug.Log("****顺序排列****");
        var tlist = list.OrderBy(t => t.sno).ToList();

Debug.Log("****倒序排列****");
        var tlist = list.OrderByDescending(t => t.sno).ToList();

方法五 chon重写Comparable

public class Student: IComparable
{
    public int sno;
    public string name;

    public Student(int sno, string name)
    {
        this.sno = sno;
        this.name = name;
    }

    //重写的CompareTo方法,根据Id排序
    public int CompareTo(Student other)
    {
        if (null == other)
        {
            return 1;//空值比较大,返回1
        }
        //return this.Id.CompareTo(other.Id);//升序
        return other.sno.CompareTo(this.sno);//降序
    }
}

或者

 public int Compare(Student x, Student y)
    {
        return x.sno.CompareTo(y.sno);//升序
    }

测试脚本如下

#region 模块信息
// **********************************************************************
// Copyright (C) 2019 Blazors
// Please contact me if you have any questions
// File Name:             Test
// Author:               
// WeChat||QQ:           
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Student: IComparable
{
    public int sno;
    public string name;

    public Student(int sno, string name)
    {
        this.sno = sno;
        this.name = name;
    }

    //重写的CompareTo方法,根据Id排序
    public int CompareTo(Student other)
    {
        if (null == other)
        {
            return 1;//空值比较大,返回1
        }
        //return this.Id.CompareTo(other.Id);//升序
        return other.sno.CompareTo(this.sno);//降序
    }
    public int Compare(Student x, Student y)
    {
        return x.sno.CompareTo(y.sno);//升序
    }

}
public class Test : MonoBehaviour
{
    List targetList;
    // Use this for initialization
    void Start()
    {
      
    }
    private void Update()
    {
        //方法一
        if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表达式
        {
            List list = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            list.Sort((x, y) => -x.CompareTo(y));//降序
            list.Sort((x, y) => x.CompareTo(y));//升序

           
        }
        //方法二
        if (Input.GetKeyDown(KeyCode.W))//简单sort排序
        {
            List list = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            list.Reverse();// 反转顺序          
            list.Sort();// 升序排序

        }
        //方法三
        if (Input.GetKeyDown(KeyCode.W))//简单sort排序
        {
            List list = new List();
            list.Sort(
                delegate (Student p1, Student p2)
                 {
                     return p1.sno.CompareTo(p2.sno);
                    
                 });
            //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });
        }
       
        //方法三
        if (Input.GetKeyDown(KeyCode.Q))//OrderBy的运用
        {
            targetList = new List();
            for (int i = 0; i < 10; i++)
            {
                targetList.Add(new Student(i, "小明" + i));
            }
            var tList01 = OutOfOrder(targetList);
            var tList02 = InOrder(tList01);
            var tList03 = OutOfOrder(tList02);
            InvertedOrder(tList03);
        }
       
    }
    private List InOrder(List list)
    {
        Debug.Log("****顺序排列****");
        var tlist = list.OrderBy(t => t.sno).ToList();
        string str = ""; ;
        foreach (var item in tlist)
        {
            str += item.sno;
        }
        Debug.Log("顺序后学号:" + str);
        return tlist;
    }
    private List InvertedOrder(List list)
    {
        Debug.Log("****倒序排列****");
        var tlist = list.OrderByDescending(t => t.sno).ToList();
        string str = ""; ;
        foreach (var item in tlist)
        {
            str += item.sno;
        }
        Debug.Log("倒序后学号:" + str);
        return tlist;
    }


    /// 
    /// List乱序
    /// 
    /// 
    /// 
    public List OutOfOrder(List a)
    {
        Debug.LogError("****打乱列表****");
        List b = new List();
        int countNum = a.Count;
        //使用while循环,保证将a中的全部元素转移到b中而不产生遗漏
        while (b.Count < countNum)
        {
            //随机将a中序号为index的元素作为b中的第一个元素放入b中
            int index = UnityEngine.Random.Range(0, a.Count - 1);
            //检测是否重复,保险起见
            if (!b.Contains(a[index]))
            {
                //若b中还没有此元素,添加到b中
                b.Add(a[index]);
                //成功添加后,将此元素从a中移除,避免重复取值
                a.Remove(a[index]);
            }
        }
        string str = ""; ;
        foreach (var item in b)
        {
            str += item.sno;
        }
        Debug.Log("乱序后学号:" + str);
        return b;
    }

}

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0395s