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
38
99
54
52
95
20
75
8
13
1
86

Sorted List of Random Numbers
1
8
13
20
38
52
54
75
86
95
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]