1

The problem is it gives the words out like this:

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

From z to a but for example the position of the words "hello" and "hilla" should been changed and I don't know why they are like this.

I know there is a compareTo function for characters. I would like to know why this is sorting the words in the array incorrectly.

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","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();
        }
    }
}
Michael Santos
  • 466
  • 7
  • 15
  • 1
    Any reason to not use `wordlist.OrderByDescending(s => s)` ? – Arnaud F. Aug 28 '17 at 20:10
  • Just use `string[] wordlist = new string[] { "hello", "hilla" };`, That way you can easily debug your code (First you swap these word because of `e` nad `i` and then you swap again because of `a` and `o`) – L.B Aug 28 '17 at 20:27
  • 1
    @Arnaud F. i wanted to do it on my own without using these little helpers :D for getting better in c# yeah :D didnt work and i didnt understand why :) – Michael Santos Aug 28 '17 at 20:28
  • @ L.B i tried this but it didnt help me i also addet about half th abc and combinations like aaaa aaab baaa ... to understand it ... didnt work :( but still : thank you for helping me :) – Michael Santos Aug 28 '17 at 20:30
  • @Stinkepeter666 I updated my comment :) – L.B Aug 28 '17 at 20:31
  • @L.B i thought they were only red/saved to new var' and only the words position in the array was changed behind the last if @ string temp = wordlist[i + 1]; wordlist[i + 1] = wordlist[i]; wordlist[i] = temp; break; can you explain me why they were changed ? or what is the mistake with the e i a and o , sorry i didnt understand that? :) – Michael Santos Aug 28 '17 at 20:38
  • @Stinkepeter666 `only the words position in the array was changed ` that is correct but after first swap your list becomes `{hilla, hello}` and your outer loops continue. now the last letter satisfies the condition and you swap again. – L.B Aug 28 '17 at 20:41
  • @Stinkepeter666 Just add this line into the `if` block. `Console.WriteLine("Swapping " + wordlist[i] + " " + wordlist[i+1] + " beacuse of " + letter0 + " " + letter1);` – L.B Aug 28 '17 at 20:49
  • @ L.B ouw :O can you explain why hilla comes before hello i dont understand it :( and can you answer it as "answer this question" so i can mark it as the solving answer :) – Michael Santos Aug 28 '17 at 20:49
  • @Stinkepeter666 Now try it by adding this line before your *if* `if (letter0 > letter1) break;` – L.B Aug 28 '17 at 20:54
  • oh wow @L.B THANK YOU SO MUCH :O :))))))))) THANK YOU :D 10/10 – Michael Santos Aug 28 '17 at 21:06
  • @Stinkepeter666 You can answer your own question – L.B Aug 28 '17 at 21:34
  • ok :))))))))))) – Michael Santos Aug 28 '17 at 21:46

1 Answers1

0

Try:

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] + " ");
}

See https://msdn.microsoft.com/en-us/library/system.array.sort(v=vs.110).aspx for more details

user1069816
  • 2,763
  • 2
  • 26
  • 43
  • thank you, i will save this site to my bookmarks ^^ :)) – Michael Santos Aug 28 '17 at 20:18
  • 1
    @user1069816 there is no way this post answers "why this is sorting the words in the array incorrectly" - not sure why you've recommend accepting this post as an answer. – Alexei Levenkov Aug 28 '17 at 20:24
  • @AlexeiLevenkov I was taking the comment that said "thank you" as an indication that this had solved the problem. – user1069816 Aug 28 '17 at 20:29
  • 1
    @user1069816 it didnt solve it BUT still helped me so ... :)) – Michael Santos Aug 28 '17 at 20:42
  • Based on accepted answer this is indeed duplicate of one of hundreds "sort string array" questions. – Alexei Levenkov Aug 28 '17 at 21:11
  • @L.B has answered the question in the comments under my post but he seams to dont want wo get der solving answer so i had to choose another one so the people here see that my problem is solved i didnt use copy and paste and i didnt find this question a second time – Michael Santos Aug 28 '17 at 21:14
  • here : "@Stinkepeter666 Now try it by adding this line before your if if (letter0 > letter1) break; – L.B 12 mins ago" – Michael Santos Aug 28 '17 at 21:17