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
51
54
23
58
67
94
19
60
58
32
56

Sorted List of Random Numbers
19
23
32
51
54
56
58
58
60
67
94


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]