Interviews9 answers
Sort an int type of array with using Sort() method in C#
9 Answers
you can use merge, insertion, bubble and quick sort algorithms
Quick link
http://betterexplained.com/articles/sorting-algorithms/
Devendra GohelPosted Dec 19, 2015, 6:28 PM EST
Array can be sorted using static method Array.Sort() which internally uses Qucksort algorithm.
// sort int array
int[] intArray = new int[5] { 7, 9, 2, 5, 3 };
Array.Sort(intArray);
// write array
foreach (int i in intArray) Console.Write(i + " "); // output: 2 3 5 7 9
Rajesh SinghPosted Dec 4, 2015, 9:28 PM EST
public static int[] SortArray(int[] array)
{
Array.Sort(array);
return array;
}
Pawan ChandPosted Sep 17, 2015, 4:53 PM EDT
public static int[] SortAnArray(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array.Length; j++)
{
var v1 = array[i];
var v2 = array[j];
if (v2 > v1)
{
array[i] = v2;
array[j] = v1;
}
}
}
return array;
}
Manu PrasadPosted Sep 15, 2015, 9:10 PM EDT
You can use 2 iteration or loop to solve this problem. First iteration for items & second for comparison.
Sujeet SumanPosted Sep 1, 2015, 9:43 PM EDT
you can use Bubble, Merge , Insertion or quick sort method...........
Pankaj Kumar ChoudharyPosted Aug 29, 2015, 8:18 AM EDT
http://www.csharp-examples.net/sort-array/
Munesh SharmaPosted Aug 4, 2015, 10:09 PM EDT
Guys its Without using Sort() method sorry for mistype
Ashwani ChaudharyPosted Jul 7, 2015, 3:57 AM EDT
using System;
using System.Collections;
namespace GenericApp
{
public class TestClass
{
// here int[] is return type
public int[] SortMyArray(int[] str1)
{
int i = 0;
int j = i + 1;
for (; i < (str1.Length - 1); i++)
{
for (int k = i + 1; k < (str1.Length - 1); k++)
{
if (str1[i] < str1[k])
{
continue;
}
else
{
int a = str1[k];
str1[k] = str1[i];
str1[i] = a;
}
}
}
return str1;
}
public static void Main(string[] aa)
{
TestClass obj = new TestClass();
// Define a int type of array which is unsorted
int[] mylist = { 22, 10, 1, 5, 3, 9, 2 };
// Check the out put before sorting
for (int i = 0; i < mylist.Length - 1; i++)
{
Console.WriteLine(mylist[i]);
}
// Check the out put after sorting
Console.WriteLine("\n After sorting int type array \n");
// Calling a method SortMyArray(mylist) which has return type as int array
// sorted the int type of array in method and override the sorted array into existing mylist array
mylist= obj.SortMyArray(mylist);
// display the sorted int array
for (int i = 0; i < mylist.Length - 1; i++)
{
Console.WriteLine(mylist[i]);
}
Console.ReadLine();
}
}
}
Ashwani ChaudharyPosted Jul 2, 2015, 9:27 AM EDT






