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.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Multidimensional_array_sort_program
{
public partial class MainForm : Form
{
DataGridView Table;
ListSortDirection Sort_Direction;
int first_column;
int second_column;
int third_column;
public MainForm()
{
InitializeComponent();
Create_And_Fill_Table();
Refresh();
}
void Create_And_Fill_Table()
{
Table = new DataGridView();
Table.Columns.Add("First Name", "Name" );
Table.Columns.Add("Last Name" , "Surname");
Table.Columns.Add("Address" , "Address" );
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 ");
first_column = 0;
second_column = 1;
third_column = 2;
Sort_Direction = ListSortDirection.Ascending;
Table.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(SortCompare);
}
void SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
e.SortResult = System.String.CompareOrdinal(
Table.Rows[e.RowIndex1].Cells[first_column].Value.ToString(),
Table.Rows[e.RowIndex2].Cells[first_column].Value.ToString());
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 (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_Table();
Show_Sorted_Data();
}
void Sort_Table()
{
Table.Sort(Table.Columns[first_column], Sort_Direction);
}
void Show_Sorted_Data()
{
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
);
}
}
}
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