0

I have a code, which makes an array for all the letters used in a txt file, named "failas.txt":

using System;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
class Program
{
     static void Main()
     {
         string failas = "failas.txt";
         string rodymas = File.ReadAllText(failas, Encoding.GetEncoding(1257));
         Console.OutputEncoding = Encoding.UTF8;
         Console.WriteLine(rodymas);
         char[] masyvas = rodymas.ToArray().Reverse().Where(c => !char.IsWhiteSpace(c)).ToArray();
         foreach (char c in masyvas)

         {
     Console.Write(c + ",");
         }
         Console.ReadLine();
    }
}

It reverses the array. However I need it not to reverse, but sort it in alphabetical order and exclude symbols like ,, ., :, ", etc. basically exclude everything that is not a letter.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 4
    Well you are reversing it. What did you expect? Edit: Also, all your `ToArray` calls are redundant. – leppie Oct 07 '15 at 18:03
  • OK, maybe I formulated it wrong. I removed Reverse from it. However there's two problems, one - I don't know how to exclude these unwanted non alphabetical symbols, and second one, the text from a txt file is encoded in 1257, so it means that non-English letters is at the and of an array. I don't know how to fix this – Socket Checker Oct 07 '15 at 18:11

3 Answers3

0

To solve the reverse and alphabetical concerns:

char[] masyvas = rodymas.ToArray().Where(c => !char.IsWhiteSpace(c)).ToArray();

masyvas.Sort(); //Will sort in place alphabetically. 

see Sort a list alphabetically

For the characters you can look at this solution using Regex

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

See also: How do I remove all non alphanumeric characters from a string except dash?

Community
  • 1
  • 1
Resber
  • 101
  • 2
  • Now it sorts it, however the problem remains the same that Uppercase letters is at the beginning of array and non-English letters is at the end. It should not matter if its uppercase or lowercase or it's non English symbols like these ą č ę ė į š ų ū ž. – Socket Checker Oct 07 '15 at 18:30
  • I am not sure if there is support specifically for the language character set you require. You might consider writing your own Sort method with your own order definitions as tedious as that may be. – Resber Oct 07 '15 at 19:12
  • This may be of interest to your answer: [EncodingInfo.GetEncoding](https://msdn.microsoft.com/en-us/library/system.text.encodinginfo.getencoding(v=vs.110).aspx) and [Comparing and Sorting Data for a Specific Culture](https://msdn.microsoft.com/en-us/library/vstudio/a7zyyk0c(v=vs.100).aspx) – Joshua Shearer Oct 07 '15 at 19:24
0
char[] masyvas = rodymas.ToArray().OrderBy(o=>o).Where(c => !char.IsWhiteSpace(c)).ToArray();

This does it for you.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • I wrote the answer when you didn't update your question. Now you have updated it, and made it interesting too. I will do it. Cheers ! – AnjumSKhan Oct 07 '15 at 18:30
0

How about this line?

        char[] masyvas = rodymas.Where(Char.IsLetter).OrderBy(Char.ToLower).ToArray();
Matt
  • 4,612
  • 1
  • 24
  • 44