按委托排序数组

本文关键字:数组 排序 | 更新日期: 2023-09-27 18:12:37

我想使用委托按字母顺序对数组进行排序。输入为

"m", "a", "d", "f", "h"

但是输出是

 a
 a
 d
 f
 h

。它是按字母顺序排列的,但是"m"不见了,"a"翻了一倍。原因是什么?

源代码:

class ArrayMethods
{
    public void sort(String[] array, t xcompare)
    {
        xcompare = compare;
        for (int i = 0; i < array.Length - 1  ; i++)
        {
            array[i] = xcompare(array[i], array[i + 1]);
        }
        foreach (var a in array)
        {
            Console.WriteLine(a);
        }
    }
    public String compare(string p, string x)
    {
        string result = "";
        if (String.Compare(p, x) < 0)
        {
            result = p;
        }
        else
        {
            result = x;
        }
        return result;
    }
}
public delegate string t(string firstString, string secondString);
class Program
{
    public static string[] names = { "m", "a", "d", "f", "h" };
    static void Main(string[] args)
    {
        ArrayMethods arrayMethods = new ArrayMethods();
        t delHandler = null;
        arrayMethods.sort(names, delHandler);
        Console.ReadKey();
    }
}

按委托排序数组

如果需要使用委托实现排序程序,请使用以下代码。基本上,你的排序算法是错误的:不是交换元素,而是替换元素。

class ArrayMethods
{
    public void sort(String[] array, t xcompare)
    {
        xcompare = compare;
        for (int i = 0; i < array.Length - 1; i++)
        {
            xcompare(array);
        }
        foreach (var a in array)
        {
            Console.WriteLine(a);
        }
    }
    public void compare(String[] array)
    {
        int n = array.Length - 1;
        for (int i = 0; i < n; i++)
        {
            for (int j = n; j > i; j--)
            {
                if (((IComparable)array[j - 1]).CompareTo(array[j]) > 0)
                {
                    string temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}
public delegate void t(String[] array);
class Program
{
    public static string[] names = { "m", "a", "d", "f", "h" };
    static void Main(string[] args)
    {
        ArrayMethods arrayMethods = new ArrayMethods();
        t delHandler = null;
        arrayMethods.sort(names, delHandler);
        Console.ReadKey();
    }
}
class ArrayMethods
{
    public void sort(String[] array, t xcompare)
    {
        xcompare = compare; * here comes the error message: Error 1 "void   
        DelegateSecondT.ArrayMethods.compare(string[])" has the wrong return type.  
        (I translated the error message in english)*
        for (int i = 0; i < array.Length - 1; i++)   
        {
            xcompare(array);
        }
        foreach (var a in array)
        {
            Console.WriteLine(a);
        }
    }
    public void compare(String[] array)
    {
        int n = array.Length - 1;
        for (int i = 0; i < n; i++)
        {
            for (int j = n; j > i; j--)
            {
                if (((IComparable)array[j - 1]).CompareTo(array[j]) > 0)
                {
                    string temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}
 public delegate string t(String[] array);
class Program
{
    public static string[] names = { "m", "a", "d", "f", "h" };
    static void Main(string[] args)
    {
        ArrayMethods arrayMethods = new ArrayMethods();
        t delHandler = null;
        arrayMethods.sort(names, delHandler);

        Console.ReadKey();
    }
}
}