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
54
58
96
90
39
14
33
39
76
99
25

Sorted List of Random Numbers
14
25
33
39
39
54
58
76
90
96
99


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]