I have an array of dates whose format is like this: 10/15/2005 or 2/10/2011.
Let’s say the array has 10 dates, some of which may be duplicates.
arrDates(9) ‘elements 0-9 represent 1 date
What is the best way to re-arrange the elements so that they are in chronological order?
For short lists (arrays) you could use an invisible ListBox, or a fabricated ADO Recordset, or you could hand-code a short sort routine. Performance usually isn’t a big issue until you have larger lists to sort.
Here is an example using a temporary Collection. It has a similar advantage to that of using a Recordset in that it can carry multivalued items, something that gets clunky fast using multidimensional arrays. To do that with a Collection you’d create a small Class containing the values (including the sort key) you need to carry along.
This particular approach could be optimized by using a binary search in place of the simple sequential search (the “J loop” here):
Of course with something like a “date” represented as a String value you’d still need to normalize it for sorting as others have mentioned. Here that is simulated by creating String values based on numbers and padding with leading zeros for normalization.