Sorting an Array

Let's write a simple program that sorts the data in an integer array. Start a new project and name it RandomNumbers. Figure 14.3 shows the form and objects for the program.

Figure 14.3. Form and objects for the RandomNumbers project.


The txtElements text box is used to hold the number of elements in the array we're going to use. The txtNumList is the text box that will display the contents of the array. For the txtNumList text box, you need to set the Multiline property to True and the Scrollbars property to Vertical . The three buttons are named btnGen , btnSort , and btnExit . Name the form frmRandomNumbers .

First, we need to define the array that will hold the integer values. Place the Dim statement so it has module scope, as shown in the following code fragment:

 Public Class frmRandomNumbers   Inherits System.Windows.Forms.Form  Dim Elements, Numbers() As Integer 

The Dim statement defines two integer variables . The Elements variable is the number of elements the user wants to use in the program. The Numbers() variable is the array of integers.

Listing 14.1 shows how Elements is set in the program.

Listing 14.1 Setting the Value of Elements
 Private Sub txtElements_Leave(ByVal sender As Object, ByVal e As_                System.EventArgs) Handles txtElements.Leave  Elements = CInt(txtElements.Text) End Sub 

Elements is set by using the CInt() conversion function to convert the number typed in by the user and assigning the result into Elements .

The Generate button is used to generate Elements random numbers and store them in the Numbers() array. The btnGen Click event code in Listing 14.2 shows how this is done.

Listing 14.2 The btnGen Click Event Code
 Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles btnGen.Click  Dim i As Integer  ReDim Numbers(Elements)     ' Set the array size  txtNumList.Text = ""       ' Clear the output text box  For i = 0 To Elements   Numbers(i) = CInt(Rnd() * 100)   txtNumList.Text &= CStr(Numbers(i)) & vbCrLf  Next i End Sub 

The procedure redimensions the Numbers() array to size Elements . The For loop is used to insert Elements random values into the Numbers() array. Because Rnd() returns values between 0 and 1, we multiply the return value by 100 to get the integer values to fall within the range of 0 to 100.

The second statement in the For statement block simply displays the random numbers in the txtNumList text box. Notice the symbolic constant vbCrLf at the end of the statement. This is the newline character that we built ourselves in several earlier programs.

Programmer's Tip


Move the cursor to the vbCrLf constant in the listing and double-click to highlight it. Now press the F1 key. This will present you with a list of the symbolic constants that Visual Basic .NET makes available to you. Any time you want information about a Visual Basic .NET keyword or function, highlighting it and pressing the F1 key will call up Visual Basic .NET's online help for the highlighted item.

If you ran the program at this point, you could get a list of random numbers to be displayed in the txtNumList text box. A sample run is shown in Figure 14.4.

Figure 14.4. A sample run of the RandomNumbers program.


In this example, we've elected to generate 12 random numbers, which we have then displayed in the output window by clicking the Generate button.

To sort those numbers, we need to examine the code associated with the btnSort Click event. This code is shown in Listing 14.3.

Listing 14.3 Code for the btnSort Click Event
 Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles btnSort.Click  Dim i As Integer  Array.Sort(Numbers)  txtNumList.Text = ""  For i = 0 To Elements   txtNumList.Text &= CStr(Numbers(i)) & vbCrLf  Next End Sub 

All the work associated with sorting the array is done with a single statement:


All arrays in Visual Basic .NET have certain methods that you can use to process the data in the array. The Sort() method is one of those methods. As you can tell from Listing 14.3, the Sort() method syntax is

 Array.Sort(  ArrayToSort  ) 

Notice that the method is prefixed with the Array class name, which is integral to Visual Basic .NET. All you have to do is supply the name of the array and the Sort() method does all the work for you. What's really nice about the Sort() method is that Visual Basic .NET is smart enough to know the type of data that has to be sorted. If you were to write your own routine, you would either need to write a separate sort routine for each data type you want to sort, or you would have to write a (more complex) generalized sort routine that could handle all data types.

After the array is sorted, a For loop is used to display the sorted list of values in the txtNumList text box. A sample run after the list is sorted is shown in Figure 14.5.

Figure 14.5. Sample run after the Sort() method is called.


Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: