【发布时间】:2017-08-28 20:06:07
【问题描述】:
问题是它会这样表达:
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();
}
}
}
【问题讨论】:
-
有什么理由不使用
wordlist.OrderByDescending(s => s)? -
只需使用
string[] wordlist = new string[] { "hello", "hilla" };,这样您就可以轻松调试代码(首先因为e和i交换这些词,然后因为a和@987654329 而再次交换@) -
@Arnaud F. 我想在不使用这些小帮手的情况下自己做:D 以便在 c# 中变得更好是的:D 没用,我不明白为什么 :)
-
@ LB 我试过这个,但它没有帮助我,我还添加了大约一半的 abc 和 aaaa aaab baaa 之类的组合......理解它......没用:(但仍然:谢谢你帮助我:)
-
@Stinkepeter666 我更新了我的评论 :)
标签: c# .net arrays sorting words