Results 1 to 12 of 12

Thread: VB6: 2-dimensional array sorter contest

Threaded View

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    VB6: 2-dimensional array sorter contest

    This is a friendly contest to see who can come up with the best two-dimensional array sorter.


    Purpose

    Generally speaking, whenever you see a multi-dimensional array with fixed dimensions, programmers tend to declare them as MyArray(Rows, Cols). That works perfectly well, and is intuitive to access. For example:

    Dim FixedArray(10,2)
    FixedArray(0,0) = 1
    FixedArray(0,1) = "John Smith"
    FixedArray(0,2) = "123 Main Street"
    FixedArray(1,0) = 2
    FixedArray(1,1) = "Jane Doe"
    FixedArray(1,2) = "96 Edgemont Drive"
    ...etc...

    The problem creeps in when you need to use a dynamic array, because you can't Redim Preserve that first dimension to add more rows; you can only redim the last dimension. So for dynamic arrays you'll usually see the opposite setup:

    Redim Preserve DynamicArray(2,10)
    DynamicArray(0,0) = 1
    DynamicArray(1,0) = "John Smith"
    DynamicArray(2,0) = "123 Main Street"
    DynamicArray(0,1) = 2
    DynamicArray(1,1) = "Jane Doe"
    DynamicArray(2,1) = "96 Edgemont Drive"
    ...etc...

    Because both setups are common, your solution must be flexible enough to handle either. To finish these examples, let's say you wanted to sort on the name column. The calls would end up looking something like this:

    SortArray FixedArray, 2, 1 ' Sort on second dimension, index 1
    SortArray DynamicArray, 1, 1 ' Sort on first dimension, index 1

    Another use is when you want to sort a large UDT array. The simplest (and fastest) approach would be to create a separate two-dimensional array of the same size as the UDT, stuff the UDT index into one of the dimensions and whatever field you wanted to sort on in the other. After sorting the index array on that field, you end up with a list of the UDT indexes in the desired order giving you a quick and easy way to loop through the UDT.

    The net result is that you'll be creating a useful and flexible indexing tool that may just come in handy some day.


    Requirements

    1) Your function must accept at least the following parameters:
    • An array to be sorted
    • The dimension that contains the column to be sorted
    • The column to be sorted
    2) Your solution must be able to sort numbers, dates and strings.

    3) Your solution can only be a single function. (Unlimited API declarations -- plus any applicable UDTs -- are allowed, but no variables outside the scope of your function.)


    Judging

    There are three main criteria that will be used for judging, plus a tiebreaker:
    • Speed is a trump card. Make it super-fast, and you'll probably win.
    • Extra features/functonality is huge. A good enough set of features can trump even speed. (eg: Sorting on multiple columns.)
    • Ease of use for the programmer trying to incorporate your solution into their project.
    • Elegance will be used as a tiebreaker.
    Note that code-stealing is allowed. Bonus points go to the originator of an idea, and minus points if you improve on somebody else's idea without giving credit.


    Time Limit

    None, until further notice.


    Prizes

    If anyone has any ideas, I'm all ears. I'm thinking at the very least I'll broadcast your achievement in a signature on all my posts for a month.
    Last edited by Ellis Dee; Apr 7th, 2007 at 08:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width