[<< wikibooks] Visual Basic for Applications/Bubble Sort on Multiple Keys
== Summary ==


== Array Sort on Three Keys ==
This rather long VBA code listing allows bubble sorting of an array on three keys. It is sometimes called an intersort.
In case it is not clear what that means, suppose there are many names to sort;  each with two forenames and a surname.   The names records occupy a row each and their parts are in separate columns.   The first key might sort the surnames column, but there could be many records called Smith. Then the second key sorts among the first forenames where the surnames were similar.   And there might still be a lot of John Smith name records that are the same. The third key sorts the second forename column for those cases where there are  records with the same surname and first forename combination.
A similar function can be found on Excel worksheets in the advanced sort functions.  Users unfamiliar with this sort type might well experiment there to better understand the process.
The function here has options for ascending or descending sorts, row sort or column sort, and the option to return the sorted work in another array or the original.   Up to three keys can be specified, though if there are unused keys, say, because only two intersorts are needed, it is assumed that Key1 and Key2 will be used before Key3.  In any case, unreasonable settings will result in message box advice.