Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Featured Discussion: Two Different Ways to Sort Arrays in VBScript : Page 2

There are often many ways to solve a programming problem, and the problem of sorting multi-dimensional arrays is no exception. This featured discussion discusses two methods of solving the same sorting problem. First, how to sort a multi-dimensional array directly, and second, how to sort the same data placed in a user-defined type (UDT).


advertisement
Solution #2
 
Figure 1: The initial (unsorted) display.

Phil Weber suggests an alternative method. Tom could use a user-defined type (UDT) to hold the data and then sort the UDT, rather than sorting with a multi-dimensional array. Simply create an array of the type; when an item is moved, all of its elements will move together. DevX Executive Editor A. Russell Jones has contributed sample code to illustrate Phil's point.

You can find many different types of sort routines on the Web and in print VB resources. The sort routines for this example are based on a QuickSort function found on the VB2TheMax site. The original version doesn't work with UDTs, but that's easy to fix. Unfortunately, it's also labor-intensive. Sort routines such as this are type specific so if your UDT contains fields of differing types, you'll need to write one sort routine for each type. In addition, to work with named fields in a UDT, you can't simply pass in the field name, and there's no way to access the fields by index number; therefore you have to hard-code the sort routines, one for each field by which you want to sort the UDT.



The example sorts an array of UDT items called state_struct, which contains census population data, by state, for the years 1990 and 2000. Each UDT item contains a state name, abbreviation, and the population figures for the two years. When you consider this data, you can see that it would be convenient to be able to display the items sorted by any field in the UDT.

Public Type state_struct name As String abbreviation As String population2000 As Long population1990 As Long End Type

 
Figure 2: Here's the same data sorted by the population2000 field.

To do that, you'll need four copies of the QuickSort routine, one for each field. In the example code, those routines are in a StateSorter.bas module, and are named QuickSortStatesByName, QuickSorrtStatesByAbbreviation, QuickSortStatesByPop1990 and QuickSortStatesByPop2000. The sample form contains buttons that let you sort the data by any field. Take a look at Figure 1 to see the initial (unsorted) display.

Figure 2 shows the same data sorted by the population2000 field, in reverse order (highest population first).

You can see that using a UDT lets you sort by any one field while still keeping the data for the rows in sync. You could easily adapt this same sorting scheme to sort a class containing properties for the fields.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date