委托 比较类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer[] c1 = new Customer[] { new Customer("zwj", 1), new Customer("zwj32", 20), new Customer("zwj2", 56) };
            Sort(c1, Customer.compar);
            foreach (var item in c1)
            {
                Console.WriteLine(item.Sale);
            }
            Console.ReadKey();
        }


        static void Sort<T>(IList<T> sortArray, Func<T, T, bool> compare)
        {

            bool swapped = true;
            do
            {
                swapped = false;
                //  sortArray.Count - 1  比较的时候 是跟后面一个比较 也就是最大的-1;
                for (int i = 0; i < sortArray.Count - 1; i++)
                {
                    if (compare(sortArray[i + 1], sortArray[i]))
                    {
                        T temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }

            } while (swapped);

        }


        class Customer
        {


            public Customer(string n1, int sale)
            {

                this.Names = n1;
                this.Sale = sale;

            }
            public int Sale { get; set; }
            public string Names { get; set; }





            public static bool compar(Customer c1, Customer c2)
            {


                return c1.Sale > c2.Sale;
            }

        }
    }
}

 

posted @ 2016-10-19 16:29  刀小爱  阅读(125)  评论(0)    收藏  举报