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
10
69
42
60
45
26
59
68
44
79
32

Sorted List of Random Numbers
10
26
32
42
44
45
59
60
68
69
79


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]