2017-08-28 71 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

相關問題