M
M
Maria2016-03-02 18:59:48
C++ / C#
Maria, 2016-03-02 18:59:48

How to sort words in a text by the number of occurrences of a given character?

There is a code (below) that sorts the words of the text alphabetically. But I can't figure out how to make it so that sorting is carried out by the number of occurrences of a given character, for example, the letter "a".
Help, please, to solve the problem.

static void Main(string[] args)
        {
            string path = @"F:\file.txt";
            string text = System.IO.File.ReadAllText(path).ToLower();
            string[] arr = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string s in arr)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("BEFORE:");
            for (int i = 0; i < arr.Length; i++) Console.WriteLine("{0}. {1}", i + 1, arr[i]);

            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length - 1; j++)
                {
                    if (needToReOrder(arr[j], arr[j + 1]))
                    {
                        string s = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = s;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("AFTER:");
            for (int i = 0; i < arr.Length; i++) Console.WriteLine("{0}. {1}", i + 1, arr[i]);
            Console.ReadKey();
        }

        protected static bool needToReOrder(string s1, string s2)
        {
            for (int i = 0; i < (s1.Length > s2.Length ? s2.Length : s1.Length); i++)
            {
                if (s1.ToCharArray()[i] < s2.ToCharArray()[i]) return false;
                if (s1.ToCharArray()[i] > s2.ToCharArray()[i]) return true;
            }
            return false;
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aminought, 2016-03-02
@aminought

Well, since you're asking. :)

String text = "aaaa aa aaaaa aaa a aaaaaaaaa aaaaaaaaaa aao";
char searchFor = 'a';
var sorted = text
   .Split(new char[] {'.', '?', '!', ' ', ';', ':', ','}, StringSplitOptions.RemoveEmptyEntries)
   .ToList()
   .OrderBy(s => s.ToCharArray().Count(c => c == searchFor))
   .ToList();
sorted.ForEach(s => Console.WriteLine(s));

A
Alexey Pavlov, 2016-03-02
@lexxpavlov

Make a Dictionary, in it in a loop increase by one for each word. The result for each word will be the number of each found word. Then convert the dictionary to a list and sort it (see example here ).

M
Maria, 2016-03-02
@missbells

I solved the problem by using bubble sort. Perhaps someone will need the code (below).

class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\file.txt";
            string text = System.IO.File.ReadAllText(path).ToLower();
            string[] array = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

            int[] countArray = new int[array.Length];

            char letter = 'o';

            for (int i = 0; i < array.Length; i++)
            {
                for (int j = 0; j < array[i].Length; j++)
                {
                    if (array[i][j] == letter)
                    {
                        countArray[i] += 1;
                    }
                }
            }

            for (int i = 0; i < countArray.Length - 1; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (countArray[j] < countArray[i])
                    {
                        var temp = countArray[i];
                        string tempSTR = array[i];


                        countArray[i] = countArray[j];
                        countArray[j] = temp;

                        array[i] = array[j];
                        array[j] = tempSTR;
                    }
                }
            }

            for (int i = 0; i < array.Length; i++)
            {
                
                //System.Console.Write(countArray[i] + "");
                System.Console.Write(array[i]+" ");
            }
            Console.ReadKey();
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question