的问题是它给出去的话是这样的:我想的话从一个数组排序,但它不工作
zahlen zahlen z y wörter w sondern sind standard s r keine k junge j i hello hilla h g f e die diese bbbb bbba bbba a
从Z到例如但是的话“你好”的位置和“希拉”应该改变,我不知道他们为什么这样。
我知道有一个compareTo
函数的字符。我想知道为什么这是错误排序数组中的单词。
using System;
using System.Collections;
namespace WortArraySortieren
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello junge die standard zahlen sind keine zahlen sondern diese wörter hier ");
string[] wordlist = new string[] {"hello","junge","die","standard","zahlen","sind","keine","zahlen","sondern","diese","wörter","hilla","a", "bbba", "bbbb", "bbba", "e", "f", "g", "h", "i", "j", "s", "w", "k", "z", "r", "y" };
int length = wordlist.Length;
for (int m = 0; m < wordlist.Length; m++)
{
for (int i = 0; i < wordlist.Length - 1; i++)
{
string a = wordlist[i];
string b = wordlist[i + 1];
for (int e = 0; e < a.Length && e < b.Length;e++)
{
char letter0 = a[e];
char letter1 = b[e];
if (letter0 < letter1)
{
string temp = wordlist[i + 1];
wordlist[i + 1] = wordlist[i];
wordlist[i] = temp;
break;
}
}
}
}
for (int u = 0; u < wordlist.Length; u++)
{
Console.Write(wordlist[u] + " ");
}
Console.ReadLine();
}
}
}
任何理由不使用'wordlist.OrderByDescending(S => S)'? –
只需使用'string [] wordlist = new string [] {“hello”,“hilla”};',这样你就可以很容易地调试你的代码(首先你交换这些单词,因为''''''''你再次交换,因为'a'和'o') –
@Arnaud F.我想自己做这件事,而不使用这些小助手:D在C#中变得更好是:D没有工作,我不明白为什么:) – Stinkepeter666