Array,List,Dictionary通过IComparer排序

Dictionary对values进行排序

[Serializable]
public class CAnimatorData
{
    public string animatorName;
    public int animatorType;
    public object parm;
    public float t;
    
}
public class CAnimatorDataSort : IComparer<CAnimatorData>
{
    //按价格排序
    public int Compare(CAnimatorData x, CAnimatorData y)
    {
        return x.t.CompareTo(y.t);
    }
}
Dictionary<string, CAnimatorData> dictAnimator = new Dictionary<string, CAnimatorData>();

void InputAnimatorCache(string ani, int type, object parm)
    {
        if (animator.isActiveAndEnabled)
            return;
        if (!dictAnimator.ContainsKey(ani))
        {
            CAnimatorData cad = new CAnimatorData();
            cad.t = Time.time;
            cad.animatorName = ani;
            cad.animatorType = type;
            cad.parm = parm;
            dictAnimator.Add(ani, cad);
        }
        else
        {
            CAnimatorData cad = new CAnimatorData();
            cad.t = Time.time;
            cad.animatorName = ani;
            cad.animatorType = type;
            cad.parm = parm;
            dictAnimator[ani] = cad;
        }
    }

    void CheckAnimatorActive()
    {
        if (dictAnimator.Count == 0)
            return;
        if (!animator.isActiveAndEnabled)
            return;
        CAnimatorData[] cary = new CAnimatorData[dictAnimator.Count];
        dictAnimator.Values.CopyTo(cary, 0);
        Array.Sort(cary, new CAnimatorDataSort());
        for (int i = 0; i < cary.Length; i++)
        {
            CAnimatorData d = cary[i];
            SetAnimator(d.animatorName, d.animatorType, d.parm);
        }
    }

List排序


List<Student> students = new List<Student>();
//Linq
List<Student> sortedStudents = students.OrderByDescending(s => s.num).ToList();


//List.Sort
students.Sort((Comparison<Student>)delegate(Student a, Student b) { return a.num > b.num ? 1 : a.num == b.num ? 0 : -1; });


//Array.Sort
Array.Sort(students.ToArray(), (Comparison<Student>)delegate(Student a, Student b) { return a.num > b.num ? 1 : a.num == b.num ? 0 : -1; });

List通过iComparer排序

//Book 类
public class Book
    {
        public Book(string inname, string inauthor, string inpublisher, int inprice)
        {
            this.Name = inname;
            this.Author = inauthor;
            this.Publisher = inpublisher;
            this.Price = inprice;
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string author;

        public string Author
        {
            get { return author; }
            set { author = value; }
        }
        private string publisher;

        public string Publisher
        {
            get { return publisher; }
            set { publisher = value; }
        }
        private int price;

        public int Price
        {
            get { return price; }
            set { price = value; }
        }
    }

//比较器类
//当s1>s2时,s1.CompareTo(s2)==1
//当s1=s2时,s1.CompareTo(s2)==0
//当s1<s2时,s1.CompareTo(s2)==-1
public class Icp:IComparer<Book>
    {
        //按书名排序
        public int Compare(Book x, Book y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

    public class IcpPrice : IComparer<Book>
    {
        //按价格排序
        public int Compare(Book x, Book y)
        {
            return x.Price.CompareTo(y.Price);
        }
    }


//main方法
static void Main(string[] args)
        {
            List<Book> list = new List<Book>();
            Book b1, b2, b3, b4;
            b1 = new Book("《绝代双骄》", "古龙", "中国长安出版社", 50);
            b2 = new Book("《鹿鼎记》", "金庸", "人民文学出版社", 120);
            b3 = new Book("《三国演义》", "罗贯中", "中州古籍出版社", 200);
            b4 = new Book("《西游记》", "吴承恩", "晨光出版社", 80);
            list.Add(b1);
            list.Add(b2);
            list.Add(b3);
            list.Add(b4);

            //按书名升序
            Console.WriteLine("/按书名升序/");
            list.Sort(new Icp());
            foreach(Book book in list)
            {
                Console.WriteLine(book.Name+book.Author+book.Publisher+book.Price);
            }
            Console.WriteLine("");

            //按价格降序序
            Console.WriteLine("/按书名升序/");
            list.Sort(new IcpPrice());
            
            foreach (Book book in list)
            {
                Console.WriteLine(book.Name + book.Author + book.Publisher + book.Price);
            }
            Console.WriteLine("");

            Console.Read();
        }

嵌套的List混合数据排序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace test1
{
    public class ListObjectComparer : IComparer<List<object>>
    {
        //按价格排序
        public int Compare(List<object> x, List<object> y)
        {
            return ((int)x[0]).CompareTo((int)y[0]);
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("--------------write : ------");
            List<List<object>> zsList = new List<List<object>>();

            List<object> one;
            one = new List<object>();
            one.Add(3);
            one.Add("name3");
            zsList.Add(one);

            one = new List<object>();
            one.Add(1);
            one.Add("name1");
            zsList.Add(one);

            one = new List<object>();
            one.Add(2);
            one.Add("name2");
            zsList.Add(one);

            zsList.Sort(new ListObjectComparer());

            //zsList.OrderBy(o => (int)(o[0])).

            string serverListString = JsonConvert.SerializeObject(zsList);

            Console.Write(serverListString);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值