The .Net IComparer Interface - Part 2


Part one described the sample application user experience.

Part two here will explain the main form engineering of the C# solution.

Part three will describe the C# solution IComparer sort machinery.

Part four will explain the C# solution data display form, and wrap up with a brief description of the differences between the C# and example applications.

Download the sample C# / software at the GitHub repository.


We'll now look at the code-behind for main form CSharp_Demo_App.cs.

The sample application opens the first form
by default, adding and configuring DataGridView object
to the form in the form Load event. Although a Visual Studio application can easily fill a DataGridView from a database, this application uses arrays. To handle the column headers, it first builds 2-D string array
at line 49, placing the RecipeDGV column names on the left and the column header text on the right.

1. Set up the RecipeDGV columns in the form load event
At line 63, array
has the RecipeDGV column width values. The for-loop at line 67 uses the two arrays to configure the RecipeDGV columns. Line 94 builds 2-D string array
which has the actual data values for RecipeDGV. The for-loop at line 114

2. Build and load the data rows for the
DataGridView in the form
load event
loads RecipeDGV from DGVRecordArray and make final configurations.

The Click event opens the second form and disables the two button controls. In this form, code in the Activated method enables the button controls.

The btn_load_RTB load event method pulls together the records for the second form, opens and shows that form, and disables the CSharp_Demo_App button controls. First, it tests for one or more selected rows in RecipeDGV at line 134. At line 178

3. Place all selected RecipeDGV row values
in a 1-D array, and declare / initialize
a ColumnSorter object
it loops through all selected RecipeDGV rows, placing all cell values in all selected rows into
1-D string array.The loop looks at one selected row as a "unit", placing those assembled values, as a finished array, in one combinedDGVRecordArray cell. It might seem like a 2-D array structure similar to DGVRecordArray would work better for this, but as we'll see later, the sorting machinery expects a 1-D array. This loop increments variable i to map the selected rows in RecipeDGV to a new cell in combinedDGVRecordArray. Line 203 declares and initializes column_comparer, a new
class object. This line first sorts by column two ascending, then by column one descending. We'll look at the ColumnSorter class later. Line 203 hard-coded the column values but we could easily build a Visual Studio application with form controls that a user could then use to set dynamic sort criteria.

Line 205 sorts the combinedDGVRecordArray array.

4. Sort the 1-D array, gather the selected
row data, and show textForm1
The line 213 loop assembles the column two (recipe_name) and column three (recipe_text) values of every combinedDGVRecordArray cell into string variable recipe_text. Note that in this loop, we could easily include values from one, some, or all available columns, in any combination and in any order. Line 216 uses variable
of lines 161 - 163 to separate the records and line 224 trims the trailing non-record characters. Line 230 declares and initializes a new
form as an object. Lines 232 and 233 disable the button controls. Finally, the method loads the assembled recipe_text value in textForm1 and shows it.

Part 2 here showed how the overall sample C# application engineering works. Part 3 will describe the core sorting machinery in the ColumnSorter class.