Tutorials > Tutorial 20 - Sorting and Searching |
In this tutorial, you will learn how to use the sorting and searching features of the XArrayDB object. The QuickSort method sorts a range of rows in an XArrayDB object according to one or more columns (up to ten). The Find method searches for a specified value within a column of an XArrayDB object, starting at a particular row
Start with a project created in Tutorial 19.
Place the following controls on the form (Form1) as shown in the following figure: two text box controls (Text1 and Text2), a command button (Command1), and two labels (Label1 and Label2).
Initializing the array data
Next, initialize the XArrayDB object so that it contains 100 rows and 4 columns of random integers.
Place the following code in the Form_Load event:
Example Title |
Copy Code
|
---|---|
Private Sub Form_Load() ' Allocate space for 100 rows, 4 columns. x.ReDim 0, 99, 0, 3 Dim row As Long, col As Integer ' The LowerBound and UpperBound properties correspond to the LBound and ' UBound functions in Visual Basic. Hard-coded dimensions can be used ' instead, if known. For row = x.LowerBound(1) To x.UpperBound(1) For col = x.LowerBound(2) To x.UpperBound(2) x(row, col) = CInt(99 * Rnd + 1) Next col Next row ' Bind True DBGrid Control to this XArrayDB instance. Set TDBGrid1.Array = x ' Enable footers. TDBGrid1.ColumnFooters = True ' Display headers and footers as buttons. Dim obcol As TrueDBGrid80.Column For Each obcol In TDBGrid1.Columns obcol.ButtonFooter = True obcol.ButtonHeader = True Next obcol End Sub |
Add the following code to Command1:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() Dim RowFound As Long ' Execute Find. RowFound = x.Find(x.LowerBound(1), CInt(Text2.Text), _ CInt(Text1.Text), _ XORDER_ASCEND, XCOMP_EQ, XTYPE_NUMBER) ' Successful Find will return a row number. Set focus to the row and ' column. If RowFound >= 0 Then TDBGrid1.Bookmark = RowFound TDBGrid1.col = CInt(Text2.Text) TDBGrid1.SetFocus End Sub |
Add the following code to the HeadClick and FootClick events:
Example Title |
Copy Code
|
---|---|
Private Sub TDBGrid1_HeadClick(ByVal ColIndex As Integer) ' Ascending sort. x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _ XORDER_ASCEND, XTYPE_INTEGER TDBGrid1.Refresh End Sub Private Sub TDBGrid1_FootClick(ByVal ColIndex As Integer) ' Descending sort. x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _ XORDER_DESCEND, XTYPE_INTEGER TDBGrid1.Refresh End Sub |
Run the program and observe the following:
The grid displays the data assigned to the XArrayDB object in code and appears as follows. (Since random numbers are used, the actual values may differ.)
Clicking a column header sorts all of the rows in ascending order based on the data within the associated column. Similarly, clicking a column footer sorts the rows in descending order.
Type one of the visible values in the last column into the Find text box ("54" in this example), "3" into the In Column text box, then click the Find button. Since a matching value exists, the grid makes the specified cell current.
Type a three-digit number into the Find text box, then click the Find button again. Since no matching value exists, the current cell does not change.
This concludes Tutorial 20.