zoukankan      html  css  js  c++  java
  • C#中List<T>中对T的Sort()

    class Program
    {
      static void Main(string[] args)
      {
        Random r = new Random();
        List<A> list_A = new List<A>();
        for (int i = 0; i < 10; i++)     {       A a = new A();       a.age = r.Next(1, 10);       list_A.Add(a);     }
        ConsolOut(list_A, "未排序:");
        list_A.Sort(new A());     ConsolOut(list_A, "排序以后:");   }      
      //输出函数   public static void ConsolOut(List<A> list, string s)   {     Console.Write(s);     for (int i = 0; i < list.Count; i++)     {       Console.Write(list[i].age + " ");     }     Console.WriteLine();   } }
    //List中的类 public class A : IComparer<A>
    {   public int age = 0;   public int Compare(A x, A y)   {     if (x.age > y.age)     {       return -1;//大的放左边     }     else if (x.age < y.age)     {       return 1;//小的放右边     }     else     {       return 0;//不变     }   } }

    结果:

    其实还有一种方法,也是需要实现接口的,都差不多。

  • 相关阅读:
    八、总结
    第5章、Kafka监控
    十一、总结
    十、图形化的客户端和监控工具
    九、zookeeper四字监控命令
    八、zookeeper 开源客户端curator介绍
    七、Zookeeper原理
    六、zookeeper 事件监听机制
    五、zookeeper的javaApi
    四、zookeeper的Acl权限控制
  • 原文地址:https://www.cnblogs.com/Transmuter/p/11102460.html
Copyright © 2011-2022 走看看