zoukankan      html  css  js  c++  java
  • [C# .NET] 数组排序 Arrays

    This example shows how to sort arrays in C#. Array can be sorted using static method Array.Sort which internally use Quicksort algorithm.

    Sorting array of primitive types

    To sort array of primitive types such as intdouble or string use method Array.Sort(Array) with the array as a paramater. The primitive types implements interface IComparable, which is internally used by the Sort method (it calls IComparable.Com­pareTo method). See example how to sort int array:

    [C#]

    // sort int array
    int[] intArray = new int[5] { 8, 10, 2, 6, 3 };
    Array.Sort(intArray);
    // write array
    foreach (int i in intArray) Console.Write(i + " ");  // output: 2 3 6 8 10
    
    

    or how to sort string array:

    [C#]

    // sort string array
    string[] stringArray = new string[5] { "X", "B", "Z", "Y", "A" };
    Array.Sort(stringArray);
    // write array
    foreach (string str in stringArray) Console.Write(str + " "); // output: A B X Y Z
    
    

    Sorting array of custom type using delegate

    To sort your own types or to sort by more sophisticated rules, you can use delegate to anonymous method. The generic delegate Comparison<T> is declared as public delegate int Comparison<T> (T x, T y). It points to a method that compares two objects of the same type. It should return less then 0 when X < Y, zero when X = Y and greater then 0 when X > Y. The method (to which the delegate points) can be also an anonymous method (written inline).

    Following example demonstrates how to sort an array of custom type using the delegate to anonynous comparison method. The custom type in this case is a class User with properties Name and Age.

    [C#]

    // array of custom type
    User[] users = new User[3] { new User("Betty", 23),  // name, age
                                 new User("Susan", 20),
                                 new User("Lisa", 25) };
    
    

    [C#]

    // sort array by name
    Array.Sort(users, delegate(User user1, User user2) {
                        return user1.Name.CompareTo(user2.Name);
                      });
    // write array (output: Betty23 Lisa25 Susan20)
    foreach (User user in users) Console.Write(user.Name + user.Age + " ");
    
    

    [C#]

    // sort array by age
    Array.Sort(users, delegate(User user1, User user2) {
                        return user1.Age.CompareTo(user2.Age); // (user1.Age - user2.Age)
                      });
    // write array (output: Susan20 Betty23 Lisa25)
    foreach (User user in users) Console.Write(user.Name + user.Age + " ");
    
    

    Sorting array using IComparable

    If you implement IComparable interface in your custom type, you can sort array easily like in the case of primitive types. The Sort method calls internally IComparable.Com­pareTo method.

    [C#]

    // custom type
    public class User : IComparable
    {
      // ...
    
      // implement IComparable interface
      public int CompareTo(object obj)
      {
        if (obj is User) {
          return this.Name.CompareTo((obj as User).Name);  // compare user names
        }
        throw new ArgumentException("Object is not a User");
      }
    }
    
    

    Use it as you sorted the primitive types in the previous examples.

    [C#]

    // sort using IComparable implemented by User class
    Array.Sort(users);  // sort array of User objects

    https://www.csharp-examples.net/sort-array/
  • 相关阅读:
    数据结构和算法(Golang实现)(9)基础知识-算法复杂度及渐进符号
    基于深度学习方法的dota2游戏数据分析与胜率预测(python3.6+keras框架实现)
    基于CBOW网络手动实现面向中文语料的word2vec
    《Machine Learning Yearing》读书笔记
    使用神经网络预测航班起飞准点率
    使用LSTM-RNN建立股票预测模型
    基于selenium+phantomJS的动态网站全站爬取
    TensorFlow保存、加载模型参数 | 原理描述及踩坑经验总结
    学习笔记--python中使用多进程、多线程加速文本预处理
    通过外汇对冲手段稳定获利的可行性验证
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/13163223.html
Copyright © 2011-2022 走看看