C# 按委托对数组排序
我想使用委托按字母顺序对数组排序。输入是C# 按委托对数组排序,c#,arrays,sorting,delegates,C#,Arrays,Sorting,Delegates,我想使用委托按字母顺序对数组排序。输入是 "m", "a", "d", "f", "h" 但结果是 a a d f h i、 e.它是按字母顺序排列的,但“m”不见了,而且“a”翻了一倍。原因是什么 源代码: class ArrayMethods { public void sort(String[] array, t xcompare) { xcompare = compare; for (int i = 0; i < arr
"m", "a", "d", "f", "h"
但结果是
a
a
d
f
h
i、 e.它是按字母顺序排列的,但“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();
}
}
类数组方法
{
公共void排序(字符串[]数组,t xcompare)
{
xcompare=比较;
for(int i=0;i
如果需要使用代理执行排序程序,请使用以下代码。基本上,排序算法是错误的:不是交换元素,而是替换元素
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();
}
}
类数组方法
{
公共void排序(字符串[]数组,t xcompare)
{
xcompare=比较;
for(int i=0;ii;j--)
{
if(((i可比较)数组[j-1])。比较到(数组[j])>0
{
字符串温度=数组[j-1];
数组[j-1]=数组[j];
数组[j]=温度;
}
}
}
}
}
公共委托void t(字符串[]数组);
班级计划
{
公共静态字符串[]名称={“m”、“a”、“d”、“f”、“h”};
静态void Main(字符串[]参数)
{
ArrayMethods ArrayMethods=新的ArrayMethods();
t delHandler=null;
排序(名称、delHandler);
Console.ReadKey();
}
}
类数组方法
{
公共void排序(字符串[]数组,t xcompare)
{
xcompare=compare;*出现错误消息:错误1“void
DelegateSecond.ArrayMethods.compare(字符串[])的返回类型错误。
(我用英语翻译了错误信息)*
for(int i=0;ii;j--)
{
if(((i可比较)数组[j-1])。比较到(数组[j])>0
{
字符串温度=数组[j-1];
数组[j-1]=数组[j];
数组[j]=温度;
}
}
}
}
}
公共委托字符串t(字符串[]数组);
班级计划
{
公共静态字符串[]名称={“m”、“a”、“d”、“f”、“h”};
静态void Main(字符串[]参数)
{
ArrayMethods ArrayMethods=新的ArrayMethods();
t delHandler=null;
排序(名称、delHandler);
Console.ReadKey();
}
}
}
您是否调试了代码以找出无法获得预期输出的原因?基本上,“这不是有效的排序算法”。您打算使用哪种排序算法?是否有任何理由不使用内置算法?顺便说一句,我强烈建议您使用更传统的名称。您为什么要尝试重新发明轮子?您没有编写排序算法。代码中的一个核心错误是覆盖一个数组元素,而不是交换其中的两个,从而使“m”消失。这是我在internet上找到的一项任务,用于练习委托。我知道有一些算法,但我想使用一个委托。汉斯·帕桑特:解决办法是什么?谢谢,但有点不对劲。程序告诉我方法compare的返回类型错误。这涉及行xcompare=compare;您是否更改了代理定义?它应该是上面定义的,你能检查一下吗?是的,可以。可能是compare方法必须返回字符串或其他东西吗?这里compare是空的,数组排序将在compare中进行,无需返回任何内容。你们能把它标记为答案吗?你们的委托定义和回调方法总是应该有相同的签名。在下面的帖子中,更改代表签名。如果您想比较方法名以给出有意义的名称,请将其更改为bubblesort;而是公共委托字符串t(字符串[]数组);请从我的帖子中复制整个代码并运行。
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();
}
}
}