How To Sort Two Dimensional Array in Selected Sort Order by Column One, Then By Column Two, Then By Column Three . . . Then By Column N?






2.75/5 (5 votes)
Sort two dimensional array by using DataGridView class
Introduction
This program was made as an example for solving most common and simplest problem, considering two dimensional arrays, among beginners in C# - Sorting data in selected sort order by column one, then by column two, then by column three ... then by column n.
Tip & Trick
For creating, populating and sorting two dimensional array, instead of Array
class, use DataGridView
class that enables automatic sort operation in combination with its SortCompare
event handler that allows customizing multi column sort algorithm. Read the program code below with its comments where everything is explained.
Using the Code
Program code attached with this tip is designed for learning.
It is well commented and comments cover every aspect of problem solving.
Read it carefully and thoroughly.
For practical use, it is recommended to download the full solution created in IDE Sharp Develop.
Open project in the same IDE or in the Visual Studio, build it and run program.
/*****************************************************************************************************
* Program name : Multidimensional array sort program *
* Program ver. : 1.0 *
* Created by : Sharp Develop *
* Code author : Peric Željko *
* Code language : C# *
* Date created : 9.12.2015 *
* Time created : 15:51 *
* *
* *
* Program Description : Program for sorting data inside two dimensional array. *
* *
* Program performs sort operation inside two dimensional array *
* where data inside same rows are correlated. *
* *
* Example: *
* We have collected personal data of employees : *
* First Name , Last Name and Address that are stored *
* inside two dimensional array called Personal Data. *
* *
* Personal Data : *
* *
* First Name Last Name Address *
* *
* John Porter 1234 Oak street, Pensacola, FL 32503 *
* John Porter 1891 3rd Street North Westlake, OH 44145 *
* William Patterson 4534 Virginia Street Dallas, GA 30132 *
* Marry Cortez 7642 Fairview Avenue Milwaukee, WI 53204 *
* John Patterson 1368 Street Road Morristown, NJ 07960 *
* Elizabet Cortez 3698 Cedar Avenue Saratoga Springs, NY 12886 *
* Marry Mosley 4575 11th Street Sacramento, CA 95820 *
* *
* SORT Ascending by First Name then by Last Name then by Address *
* *
* Sorted Personal Data : *
* *
* First Name Last Name Address *
* *
* Elizabet Cortez 3698 Cedar Avenue Saratoga Springs, NY 12886 *
* John Patterson 1368 Street Road Morristown, NJ 07960 *
* John Porter 1234 Oak street, Pensacola, FL 32503 *
* John Porter 1891 3rd Street North Westlake, OH 44145 *
* Marry Cortez 7642 Fairview Avenue Milwaukee, WI 53204 *
* Marry Mosley 4575 11th Street Sacramento, CA 95820 *
* William Patterson 4534 Virginia Street Dallas, GA 30132 *
* *
* *
* Instead of Array class, program uses DataGridView class for generating *
* two dimensional table for storing data, and SortCompare event handler *
* that enables to programmatically customize multicolumn data sort algorithm. *
* For sorting large amount of diverse data this would be one of the best *
* ways, especially because of simplicity and respective sorting speed. *
* *
* *
* Notice: Names and Addresses used inside program are fictional. *
* *
* All the best, *
* Author *
* *
*****************************************************************************************************/
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Multidimensional_array_sort_program
{
public partial class MainForm : Form
{
//
// Global variables
//
// Table for storing personal data of employees
DataGridView Table;
// Variable for setting data sort order inside table columns
ListSortDirection Sort_Direction;
// Variables for storing table column indexes
int first_column;
int second_column;
int third_column;
//
//
//
public MainForm()
{
InitializeComponent();
// Initialize table
Create_And_Fill_Table();
Refresh();
}
void Create_And_Fill_Table()
{
//
// Create table
//
Table = new DataGridView();
//
// Add columns
//
// DataGridView.Columns.Add("Column Name", "Column Header Text");
//
Table.Columns.Add("First Name", "Name" );
Table.Columns.Add("Last Name" , "Surname");
Table.Columns.Add("Address" , "Address" );
//
// Add and Fill rows with data
//
// Table.Rows.Add("Column1 - Cell Value", "Column2 - Cell Value", "Column3 - Cell Value");
//
Table.Rows.Add("John ", "Porter ", "1234 Oak street, Pensacola, FL 32503 ");
Table.Rows.Add("John ", "Porter ", "1891 3rd Street, North Westlake, OH 44145 ");
Table.Rows.Add("William ", "Patterson", "4534 Virginia Street, Dallas, GA 30132 ");
Table.Rows.Add("Marry ", "Cortez ", "7642 Fairview Avenue, Milwaukee, WI 53204 ");
Table.Rows.Add("John ", "Patterson", "1368 Street Road, Morristown, NJ 07960 ");
Table.Rows.Add("Elizabet ", "Cortez ", "3698 Cedar Avenue, Saratoga Springs, NY 12886 ");
Table.Rows.Add("Marry ", "Mosley ", "4575 11th Street, Sacramento, CA 95820 ");
//
// Important:
//
// Data stored inside array created as DataGridView class–type object
// are always represented (memorized) as type of String.
// When sorting, all data are compared as strings, numbers also.
// If there is need to sort numbers, it is necessary to
// adjust format of data, so that sort routine returns correct result.
//
// numerical formated sorted
// data for sorting correctly
// as strings as numbers as numbers
//
// "1" "0001" "0001"
// "10" "0010" "0003"
// "214" "0214" "0004"
// "3" "0003" "0005"
// "4" "0004" "0010"
// "5" "0005" "0214"
// "1024" "1024" "1024"
//
//
// numerical not formated sorted
// data for sorting correctly
// as strings as numbers as strings
//
// "1" "1" "1"
// "10" "10" "10"
// "214" "214" "1024"
// "3" "3" "214"
// "4" "4" "3"
// "5" "5" "4"
// "1024" "1024" "5"
//
//
// Example of formating numerical data:
//
// int numerical_data = 100;
// string format = "000000";
// string formated_data = data.ToString(format);
//
// Result of formating:
//
// numerical data formated
//
// 100 000100
//
//
//
// Set columns indexes
//
first_column = 0;
second_column = 1;
third_column = 2;
//
// Set data Sort Direction
//
Sort_Direction = ListSortDirection.Ascending;
//
// Add SortCompare event handler
//
Table.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(SortCompare);
//
//
//
}
void SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
//
// SortCompare event handler
//
//
// This event is raised every time
// when program compares two cell values to perform sort operation.
// We can use this event to sort rows inside table as we find appropriate.
//
//
//
// Example 1:
//
// e.SortResult = System.String.CompareOrdinal(
// Table.Rows[e.RowIndex1].Cells[first_column].Value.ToString(),
// Table.Rows[e.RowIndex2].Cells[first_column].Value.ToString());
//
// e.Handled = true;
//
// This would sort all rows in table by the values in the first column,
// in selected sort order
//
//
//
// Example 2:
//
// e.SortResult = System.String.CompareOrdinal(
// Table.Rows[e.RowIndex2].Cells[first_column].Value.ToString(),
// Table.Rows[e.RowIndex1].Cells[first_column].Value.ToString());
//
// e.Handled = true;
//
// This would sort all rows in table by the values in the first column,
// in the opposite sort order from selected
//
//
//
// Example 3:
//
// e.SortResult = System.String.CompareOrdinal(
// Table.Rows[e.RowIndex2].Cells[second_column].Value.ToString(),
// Table.Rows[e.RowIndex1].Cells[second_column].Value.ToString());
//
// If values are equal
// Compare values inside cells in second column
// if (e.SortResult == 0)
// {
// e.SortResult = System.String.CompareOrdinal(
// Table.Rows[e.RowIndex1].Cells[first_column].Value.ToString(),
// Table.Rows[e.RowIndex2].Cells[first_column].Value.ToString());
// }
// e.Handled = true;
//
// This would sort all rows in table
// by the values in the second column in the opposite sort order from selected,
// and then by the values in the first column in the selected sort order
//
//
//
// By swap e.RowIndex1 and e.RowIndex2 we control sort order.
//
// By changing value for column index ,
// first_column, second_column or third_column,
// we control which column cell values are relevant for sorting.
//
//
//
// Sort by first then by second then by third column
// in the selected Sort Direction
//
// Compare values inside cells in first column
e.SortResult = System.String.CompareOrdinal(
Table.Rows[e.RowIndex1].Cells[first_column].Value.ToString(),
Table.Rows[e.RowIndex2].Cells[first_column].Value.ToString());
// If values are equal
// Compare values inside cells in second column
if (e.SortResult == 0)
{
e.SortResult = System.String.CompareOrdinal(
Table.Rows[e.RowIndex1].Cells[second_column].Value.ToString(),
Table.Rows[e.RowIndex2].Cells[second_column].Value.ToString());
// If values are equal
// Compare values inside cells in third column
if (e.SortResult == 0)
{
e.SortResult = System.String.CompareOrdinal(
Table.Rows[e.RowIndex1].Cells[third_column].Value.ToString(),
Table.Rows[e.RowIndex2].Cells[third_column].Value.ToString());
}
}
e.Handled = true;
}
void Start_Sort_Click(object sender, EventArgs e)
{
// Sort data inside table
Sort_Table();
// Show sorted data in the message box
Show_Sorted_Data();
}
void Sort_Table()
{
//
// Sort table
//
Table.Sort(Table.Columns[first_column], Sort_Direction);
}
void Show_Sorted_Data()
{
// Show sorted data in the message box
MessageBox.Show(Table[0,0].Value + "\t" + Table[1,0].Value + "\t" + Table[2,0].Value + "\n" +
Table[0,1].Value + "\t" + Table[1,1].Value + "\t" + Table[2,1].Value + "\n" +
Table[0,2].Value + "\t" + Table[1,2].Value + "\t" + Table[2,2].Value + "\n" +
Table[0,3].Value + "\t" + Table[1,3].Value + "\t" + Table[2,3].Value + "\n" +
Table[0,4].Value + "\t" + Table[1,4].Value + "\t" + Table[2,4].Value + "\n" +
Table[0,5].Value + "\t" + Table[1,5].Value + "\t" + Table[2,5].Value + "\n" +
Table[0,6].Value + "\t" + Table[1,6].Value + "\t" + Table[2,6].Value + "\n",
" Sorted Personal Data",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly
);
}
}
}
/************************************
* List Of Revisions *
************************************
* *
* *
* *
************************************/
Points of Interest
Learn how to use DataGridView
class for creating simple two dimensional array and use it for sorting diverse data with all benefits of SortCompare
event handler that allows to programmatically customize multicolumn data sort algorithm. For sorting large amount of diverse data, this would be one of the best ways, especially because of simplicity and respective sorting speed.
History
- Last revision 11.10.2016 Author