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