问题是它给出了这样的话:
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到a但是例如“hello”和“hilla”这两个词的位置应该改变了,我不知道他们为什么会这样。
我知道角色有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();
}
}
}
答案 0 :(得分:0)
尝试:
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" };
Array.Sort(wordlist, StringComparer.InvariantCulture);
for (int u = 0; u < wordlist.Length; u++)
{
Console.Write(wordlist[u] + " ");
}
有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/system.array.sort(v=vs.110).aspx