行易软件

少谈技术,多谈应用

博客园 首页 新随笔 联系 订阅 管理

汇总如下:

using System;
using System.Collections;
using System.Linq;


namespace SortDemo
{
    public class MySort2 : IComparer
    {
        public int Compare(object x, object y)
        {
            return (int)y - (int)x;
        }
    }
    public class MySort3 : IComparer
    {      
        int IComparer.Compare(object x, object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(y,x));
        }
    }    

    class Program
    {
        static void Main(string[] args)
        {

            //方法1:
            int[] iArray1 = { 11, 2, 66, 33, 100, 55 };         
            Array.Sort(iArray1);
            Array.Reverse(iArray1);
            Console.WriteLine("-------1------");
            foreach (var e in iArray1)
            {
                Console.WriteLine(e);
            }
            //方法2:
            Console.WriteLine("-------2------");
            int[] iArray2 = { 11, 2, 66, 33, 100, 55 };
            MySort2 mySort2 = new MySort2();
            Array.Sort(iArray2, mySort2);
            foreach (var e in iArray2)
            {
                Console.WriteLine(e);
            }
            //方法2:
            Console.WriteLine("-------3-------");
            int[] iArray3 = { 11, 2, 66, 33, 100, 55 };
            MySort3 mySort3 = new MySort3();
            Array.Sort(iArray3, mySort3);
            foreach (var e in iArray3)
            {
                Console.WriteLine(e);
            }
            //方法4:
            Console.WriteLine("--------4-------");
            int[] iArray4= { 11, 2, 66, 33, 100, 55 };
            var iA4 = iArray4.OrderByDescending(i => i).Select(i => i);
            foreach (var e in iA4)
            {
                Console.WriteLine(e);
            }
            //方法5:
            Console.WriteLine("--------5-------");
            int[] iArray5 = { 11, 2, 66, 33, 100, 55 };
            var iA5 = from e in iArray4
                      orderby e descending
                      select e;
            foreach (var e in iA5)
            {
                Console.WriteLine(e);
            }
        }
    }
}

 

posted on 2022-11-17 18:14  行易软件技术团队  阅读(28)  评论(0编辑  收藏  举报