What are arrays and how to sort Arrays in C# 28th Nov 2022

sorting

Arrays are collection of items of a single type stored at contiguous memory locations. They are broadly used in most programming languages. Each item in an array is called an element, and each element is accessed by it's numerical index.

The main idea of writing this blog was to discuss sorting of different type of arrays.But then I thought before discussing sorting, we need to discuss a bit about arrays,atleast that can get us going on sorting.Therefore,before I go any further, we need to have a little understanding of "Jagged Arrays" and "Rectangular Arrays".Let's discuss them below.

Jagged Arrays

A Jagged array is an array, whose elements are again "arrays", but interesting part is that each of these elements can be array of different sizes.You can think of it as "array of arrays".And since it's an array of arrays, its elements are reference types and are initialized to "null". For eg say if you do


                            int [] [] jaggedArrayEg= new int[4][]
                        

Basically what we are saying above is that we are creating an 1d array(array of arrays) of size 4. And each entry in the array is a reference to an array of "int". i.e each of these elements(4 of them) can in turn save arrays of different sizes and occupy their own block in memory.Remember, each inner array must be created manually. Eg say


                        jaggedArrayEg[0]=new int[4]
                        jaggedArrayEg[1]=new int[3]
                        jaggedArrayEg[2]=new int[7]
                        jaggedArrayEg[3]=new int[2]
                         

A quick image I came up with for your reference

JaggedArray

One point with jagged array which I used to get confused initially was creating something like below.

int[][] TestArray1=new int[10][9];

Above i.e TestArray1 declaration won't work because technically it is a 1D array, and therefore you can't declare the 2nd index at time of actually creating the array.Therefore,above will result in a compilation error.You can't create a int[10][9] because each sub-array has to be initialized separately,as they are separate objects.

However,the below code will just work fine.If you are wondering why below works, again it's the same reason as above and key to remember is that "TestArray2" is NOT a 2D array, instead it is 1D array (of arrays) and therefore you can only specify one index

int[][] TestArray2=new int[10][];

Now that we know what Jagged arrays are,it's time to discuss "2D array(Multidimensional)" which is a uniform array.

Multidimensional Array

Arrays can have more than one dimension.For example, below is a uniform 2D array. As the name suggests it is used to solve problem that requires 2 dimensions i.e row and column.

int[,] are called rectangular arrays, which are declared using commas to separate each dimension.Eg below, I will show you how to declare,initialise and access.Remember that, to access or modify a two dimensional array, you have to pass both the dimensions. For eg, in the code below, if you want to access number "9", then you will have to get to that using "Two-DimensionArray[0][3]". "Two-DimensionArray[0][3]" refers to an item on the first row ,fourth column.


                    int[,] array = new int[4, 2];//Just Declare
                    //2-Dimensional Array
                    int[,] twoDimensionArray = new int[,] { {1,3},{5,6},{7,1},{2,9},{1,11} };//Declare and Initialise
                    //Access array
                    System.Console.WriteLine(twoDimensionArray[0, 0]); //prints 1
                    System.Console.WriteLine(twoDimensionArray[0, 1]); //prints 3
                    System.Console.WriteLine(twoDimensionArray[1, 0]); //prints 5
                    System.Console.WriteLine(twoDimensionArray[1, 1]); //prints 6
                    System.Console.WriteLine(twoDimensionArray[4, 0]); //prints 1
                    System.Console.WriteLine(twoDimensionArray[4, 1]); //prints 11

Below is a simple image of a 2D Array we saw above,i.e twoDimensionArray. Basically, it has 5 rows and 2 columns and that is what is depicted in the image

RectangularArray

Now that we know about what Jagged,Rectangular,Multidimenstional arrays are, lets see how we can sort them.Below,let's see how to sort 1D arrays.

It's quite easy to sort a single-dimensional array.There is this Sort method in the .Net Array class.Below,I have an array that needs to be sorted(in my Program.cs)

Sort 1D int array

                                int []singleDimensionalArr = new int[] { 2, 1, 55, 11, 4, 7, 8 };
                                SortArray.Sort1DIntArray(singleDimensionalArr);
                            

Below is my method in SortArray class.This will sort the array

      
                                public static int[] Sort1DIntArray(int[]a)
                                    {
                                     Array.Sort(a);
                                     return a;
                                    }
                    
Sort 1D string array

Same thing can be applied for a "string array" as well Below,I have a string array that needs to be sorted(in my Program.cs)


                                string[] singleDimensionalStringArr = new string[] { "Carrot", "Apple", "Grapes", "Banana", "Avocado", "Grapefruit", "Beetroot" };
                                SortArray.Sort1DStringArray(singleDimensionalStringArr);
                     

Below is my method in SortArray class


                        public static string[] Sort1DStringArray(string[] a)
                                {
                                Array.Sort(a);
                                return a;
                                }
                    

So far there is nothing fancy. It's just Array's Sort method doing the trick for us so far. For brevity,I will finish this blog here. I will discuss sorting in a bew blog all together. Link to that blog How-to-sort-jagged-and-rectangular-arrays.html

.
Conclusion.

To conclude,I hope you would have got a better understanding of Arrays in general.You would have also got how to sort a Single Dimensional Array in C#.

GitHub Repo Link >

Email me at "techspacedeck@gmail.com" incase you have queries. Alternatively, you can fill the "CONTACT" form or drop a comment below

Did you like the blog or have questions, please add your comments