2017-08-28 74 views
0

的问题是它给出去的话是这样的:我想的话从一个数组排序,但它不工作

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(); 
     } 
    } 
} 
+1

任何理由不使用'wordlist.OrderByDescending(S => S)'? –

+0

只需使用'string [] wordlist = new string [] {“hello”,“hilla”};',这样你就可以很容易地调试你的代码(首先你交换这些单词,因为''''''''你再次交换,因为'a'和'o') –

+1

@Arnaud F.我想自己做这件事,而不使用这些小助手:D在C#中变得更好是:D没有工作,我不明白为什么:) – Stinkepeter666

回答

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更多细节

+0

谢谢你,我将这个网站保存到我的书签^^ :)) – Stinkepeter666

+1

@ user1069816有没有办法这个帖子回答“为什么这是排序错误的数组中的单词” - 不知道为什么你已经建议接受这个帖子作为答案。 –

+0

@AlexeiLevenkov我正在接受评论说“谢谢”,表明这已经解决了这个问题。 – user1069816

相关问题