Array Sorting Demo

This demo illustrates how to sort an array whose elements implement the IComprable interface. All of the primitive data types in .NET - String, Integer, Double, etc. - implement IComparable and therefore arrays of these types can be sorted using Array.Sort(ArrayName).


Unsorted List of Random Numbers
12
38
69
37
32
45
92
66
12
83
11

Sorted List of Random Numbers
11
12
12
32
37
38
45
66
69
83
92


Source Code
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    Dim randNumbers(10) as Integer
    Dim i as Integer
    Dim rndNum as New Random()
    
    For i = 0 to 10
      randNumbers(i) = rndNum.Next(100)
    Next i
    
    ' Display the random numbers
    randNumDisplay.DataSource = randNumbers
    randNumDisplay.DataBind()
    
    ' Sort the array
    Array.Sort(randNumbers)
    
    ' Display the sorted array
    orderedNumDisplay.DataSource = randNumbers
    orderedNumDisplay.DataBind()
  End Sub
</script>

<b>Unsorted List of Random Numbers</b><br />
<asp:DataGrid runat="server" id="randNumDisplay" ShowHeader="False" />
<p>
<b>Sorted List of Random Numbers</b><br />
<asp:DataGrid runat="server" id="orderedNumDisplay" ShowHeader="False" />      
  

[Return to the article]