我试图对数组中的单词进行排序,但它不起作用
I tried to sort words from an array but it doesn't work
问题是它会这样说出来:
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();
}
}
}
尝试:
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
问题是它会这样说出来:
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();
}
}
}
尝试:
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