Tutorials > Tutorial 17 - 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 a new project.
Add the following controls on the form (Form1) as shown in the following figure: two text box controls (Text1 and Text2), a command button (Command1), two labels (Label1 and Label2), and a TDBList control (TDBList1).
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 (Visual Basic) 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 DBList Control to this XArrayDB instance Set TDBList1.Array = x ' Enable footers TDBList1.ColumnFooters = True ' Display headers and footers as buttons Dim obcol As TrueDBList80.Column For Each obcol In TDBList1.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 If RowFound >= 0 Then TDBList1.Bookmark = RowFound TDBList1.SetFocus End Sub |
Add the following code to the HeadClick and FootClick events:
Example Title |
Copy Code
|
---|---|
Private Sub TDBList1_HeadClick(ByVal ColIndex As Integer) ' Ascending sort x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _ XORDER_ASCEND, XTYPE_INTEGER TDBList1.Refresh End Sub Private Sub TDBList1_FootClick(ByVal ColIndex As Integer) ' Descending sort x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _ XORDER_DESCEND, XTYPE_INTEGER TDBList1.Refresh End Sub |
Run the program and observe the following:
The list 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, and then click the Find button. Since a matching value exists, the list makes the specified row current.
Type a three-digit number into the Find text box, and then click the Find button again. Since no matching value exists, the current row does not change.
This concludes Tutorial 17.