Interviews - C# Corner

Ashwani Chaudhary
Sort an int type of array with using Sort() method in C#
By Ashwani Chaudhary in C# onJul 02 2015
  • Devendra  Gohel
    Dec, 2015 19

    you can use merge, insertion, bubble and quick sort algorithmsQuick link http://betterexplained.com/articles/sorting-algorithms/

    • 0
  • Rajesh Singh
    Dec, 2015 5

    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

    • 0
  • Pawan Chand
    Sep, 2015 17

    public static int[] SortArray(int[] array){Array.Sort(array);return array;}

    • 0
  • Manu Prasad
    Sep, 2015 16

    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;}

    • 0
  • Sujeet Suman
    Sep, 2015 2

    You can use 2 iteration or loop to solve this problem. First iteration for items & second for comparison.

    • 0
  • Pankaj  Kumar Choudhary
    Aug, 2015 29

    you can use Bubble, Merge , Insertion or quick sort method...........

    • 0
  • Munesh Sharma
    Aug, 2015 5

    http://www.csharp-examples.net/sort-array/

    • 0
  • Ashwani Chaudhary
    Jul, 2015 7

    Guys its Without using Sort() method sorry for mistype

    • 0
  • Ashwani Chaudhary
    Jul, 2015 2

    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 unsortedint[] mylist = { 22, 10, 1, 5, 3, 9, 2 };// Check the out put before sortingfor (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 arraymylist= obj.SortMyArray(mylist);// display the sorted int array for (int i = 0; i < mylist.Length - 1; i++){Console.WriteLine(mylist[i]);}Console.ReadLine();}} }

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS