委托:利用委托作为排序条件的例子

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  AspNetTest.Common
{
    
/// <summary>
    
/// Define_Delegate 的摘要说明。
    
/// </summary>

    public class Define_Delegate_BubbleSort : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//以下写法也可以,但需要将 SortOrderASC和SortOrderDESC改为非静态方法
            
//DelegateSortApp dsa = new DelegateSortApp();
            
//dsa.SortASC();
            
//dsa.SortDESC();
            DelegateSortApp.SortASC();
            DelegateSortApp.SortDESC();
            
            
// 在此处放置用户代码以初始化页面
        }


        
Web 窗体设计器生成的代码
    }

    
public class BubbleSort
    
{
        
public delegate bool Order(object first, object second);
        
        
public void Sort(Array table, Order order)
        
{
            
if(order == null)
            
{
                
throw new ArgumentException();
            }

            
bool NotAllSort = true;
            
while(NotAllSort)
            
{
                
int pass = 1;
                NotAllSort 
= false;
                
for(int i=0; i<table.Length-pass; i++)
                
{
                    
if(order(table.GetValue(i), table.GetValue(i+1)))
                    
{
                        
object temp = table.GetValue(i);
                        table.SetValue(table.GetValue(i
+1), i);
                        table.SetValue(temp, i
+1);
                        NotAllSort 
= true;
                    }

                }

                
++pass;
            }

        }

    }

    
public class DelegateSortApp
    
{
        
static int[] SortTable = {5,4,6,2,8,9,1,3,7,0};
        
public static void SortASC()
        
{
            HttpContext.Current.Response.Write(
"原数据:");
            
foreach(int i in SortTable)
            
{
                HttpContext.Current.Response.Write(i 
+ " ");
            }

            HttpContext.Current.Response.Write(
"<br>");
            HttpContext.Current.Response.Write(
"升序排序:");
            
            BubbleSort bubbleSort 
= new BubbleSort();
            BubbleSort.Order order 
= new BubbleSort.Order(SortOrderASC);
            bubbleSort.Sort(SortTable, order);
            
foreach(int i in SortTable)
            
{
                HttpContext.Current.Response.Write(i 
+ " ");
            }

            HttpContext.Current.Response.Write(
"<br>");
        }

        
public static void SortDESC()
        
{
            HttpContext.Current.Response.Write(
"降序排序:");
            BubbleSort bubbleSort 
= new BubbleSort();
            BubbleSort.Order order 
= new BubbleSort.Order(SortOrderDESC);
            bubbleSort.Sort(SortTable, order);
            
foreach(int i in SortTable)
            
{
                HttpContext.Current.Response.Write(i 
+ " ");
            }

        }

        
public static bool SortOrderASC(object first, object second)
        
{
            
int firstInt = (int)first;
            
int secondInt = (int)second;
            
return firstInt > secondInt; 
        }

        
public static bool SortOrderDESC(object first, object second)
        
{
            
int firstInt = (int)first;
            
int secondInt = (int)second;
            
return firstInt < secondInt; 
        }

    }

    
}

你可能感兴趣的:(排序)