C# tutorials
9. Frequency count of array values ii
Sorting the result

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.


To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag a Button to Form 1.

6. Drag a Rich Textbox to Form 1. Resize the textbox if you like.

7. Double-click on the button. The file Form1.cs will open with the caret ready to write code that will run when button 1 is clicked.

8. Write or paste this code where the caret is:

string[] arr = { "count", "me", "and", "me", "and", "me", "as", "well" };
Dictionary<string, int> dic = new Dictionary<string, int>();
dic = mkCount(arr, dic);
richTextBox1.Text = mkSort(dic, "", " ");
return;

9. Do step 3 of 8. Frequency count of array values i.

10. Write or paste this code right below the code you wrote or copied in step 3:

public string mkSort (Dictionary<string, int> dic, string sortOrder, string separator)
{
  var sortAlpha = (from entry in dic orderby entry.Key ascending select entry);
  StringBuilder sb = new StringBuilder();
  if (sortOrder == "desc")
    {
    var sort = (from entry in sortAlpha orderby entry.Value descending select entry);
    foreach (KeyValuePair<string, int> pair in sort)
    {
      sb.AppendLine(string.Format("{0}" + separator + "{1}", pair.Value, pair.Key));
    }
  }
  else if (sortOrder == "asc")
  {
    var sort = (from entry in sortAlpha orderby entry.Value ascending select entry);
    foreach (KeyValuePair<string, int> pair in sort)
    {
      sb.AppendLine(string.Format("{0}" + separator + "{1}", pair.Value, pair.Key));
    }
  }
  else
  {
    foreach (KeyValuePair<string, int> pair in sortAlpha)
    {
      sb.AppendLine(string.Format("{0}" + separator + "{1}", pair.Key, pair.Value));
    }
  }
  return sb.ToString();
}

11. Press F5 to start debugging the program.

12. Click on the button. This text should appear in the textbox:

and 2
as 1
count 1
me 3
well 1

13. Change line 4 in the first code snippet to:

richTextBox1.Text = mkSort(dic, "desc", " ");

14. Press F5 to start debugging the program.

15. Click on the button. This text should appear in the textbox:

3 me
2 and
1 as
1 count
1 well

16. Change line 4 in the first code snippet to:

richTextBox1.Text = mkSort(dic, "asc", " ");

17. Press F5 to start debugging the program.

18. Click on the button. This text should appear in the textbox:

1 as
1 count
1 well
2 and
3 me

Comments on the first code snippet

Line 1: The array arr is created.

Line 2: The dictionary dic is created.

Line 3: The frequency of each value in the array arr is counted with the user-defined method mkCount.

Line 4: The dictionary dic is sorted with the user-defined method mkSort and written to the textbox.

Comments on the second code snippet

Line 1-29: Code for the user-defined method mkSort. With this method, you can sort a dictionary numerically (either from higher to lower values or from lower to higher values) or alphabetically. You can also choose how to separate keys and values (with a space, a tab or whatever).

Line 3: The dictionary is sorted alphabetically.

Line 5-12: If sortOrder in line 1 is set to desc, the var sortAlpha is sorted from higher to lower values and the stringbuilder sb filled with sorted keys and values from the var sort. Because the dictionary is sorted alphabetically first, keys with the same value are sorted alphabetically.

Line 13-20: If sortOrder in line 1 is set to asc, the var sortAlpha is sorted from lower to higher values and the stringbuilder sb filled with sorted keys and values from the var sort. Because the dictionary is sorted alphabetically first, keys with the same value are sorted alphabetically.

Line 21-27: If sortOrder in line 1 is set to anything but desc or asc, for example , the stringbuilder sb is filled with sorted keys and values from the var sortAlpha.

Line 28: The streambuilder is changed to a string.

More

See also the comments on C# tutorials: 8. Frequency count of array values i.


Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.