的問題是它給出去的話是這樣的:我想的話從一個數組排序,但它不工作
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