【问题标题】:I tried to sort words from an array but it doesn't work我试图对数组中的单词进行排序,但它不起作用
【发布时间】: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 =&gt; s)
  • 只需使用string[] wordlist = new string[] { "hello", "hilla" };,这样您就可以轻松调试代码(首先因为ei 交换这些词,然后因为a 和@987654329 而再次交换@)
  • @Arnaud F. 我想在不使用这些小帮手的情况下自己做:D 以便在 c# 中变得更好是的:D 没用,我不明白为什么 :)
  • @ LB 我试过这个,但它没有帮助我,我还添加了大约一半的 abc 和 aaaa aaab baaa 之类的组合......理解它......没用:(但仍然:谢谢你帮助我:)
  • @Stinkepeter666 我更新了我的评论 :)

标签: c# .net arrays sorting words


【解决方案1】:

试试:

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

【讨论】:

  • 谢谢你,我会把这个网站保存到我的书签中^^ :))
  • @user1069816 这篇文章无法回答“为什么这会错误地对数组中的单词进行排序” - 不知道你为什么建议接受这篇文章作为答案。
  • @AlexeiLevenkov 我认为“谢谢”的评论表明这已经解决了问题。
  • @user1069816 它没有解决它但仍然帮助我...... :))
  • 根据接受的答案,这确实是数百个“排序字符串数组”问题之一的重复。
猜你喜欢
  • 1970-01-01
  • 2021-03-21
  • 2020-08-29
  • 2020-09-04
  • 2021-11-05
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多