HiveBrain v1.2.0
Get Started
← Back to all entries
snippetModerate

Multi-dimensional array sort and filter functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functionsmultiarrayandfilterdimensionalsort

Problem

As I said in my last post Multi-dimensional ascending and descending sort array function, I'm back with the final version of the code.

Added features:

  • Filter array function by following parameters: GraterThan, LessThan, Equals, NotEquals, Contains, DoesNotContain, ContainRegex, DoesNotContainRegex, BeginsWith, EndsWith, and the list could be completed as needed based on the same logic.



  • Necessary regex functions (adapted from code found on the Internet and requires Microsoft VBScript Regular Expressions 5.5 library).



  • All array functions are extended to 31 dimensions.



  • Existing intermediary functions are versatile; it could be used for various array operations.



  • The code works fast even with large amounts of data.



The code does not contain any error handling, because, depending on usage purposes, it can be designed in various shapes.

```
Option Explicit

Public Function SortArray(ByRef arr As Variant, _
ByVal selPoint As Variant, _
ByRef selDim As Integer, _
Optional ByRef ascend As Boolean = True) As Variant
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Multi Dimensional Ascending and Descending Sort Array Function
'Inputs:
'1) arr = one to 31 dimensional symmetrical array
'2) selPoint = selected point index as string e.g. "arr(1,15,4)" or just "(1,15,4)"
'3) selDim = selected dimension, integer from 1 to 31
'4) ascend = Optional ascending or descending direction (default = ascending)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim seq As Variant, seqArr As Variant, idx As Variant, s As Variant, d As Variant
Dim i As Integer, arrDim As Integer
Dim LB As Long, UB As Long

LB = LBound(arr, selDim)
UB = UBound(arr, selDim)
ReDim seqArr(UB)
' counting input array dimensions
On Error Resume Next
Do
arrDim = arrDim + 1

Solution

Stop. Close Excel. Take a deep breath.

Now go and get an actual Database. MS Access. MySQL is free and Open-Source. It doesn't really matter. What matters is that Excel is not the program to be doing this in. At all. 10-Dimensions was already excessive. 31 is a severe case of Over-Engineering.

Unless you've been specifically required to include support for that number of dimensions, it's almost certainly a waste of time and development effort to do so.

Just think for a second about what a 31-dimensional array would involve. Assume that every dimensions has just 2 items. your 31-dimensional array now has 2^31 or ~1 Billion variables. VBA tends to run out of memory at around 100 Million variables (yet alone doing anything with them).

You are Never going to need to sort a 31-dimensional array. You are never even going to see one. I can't think of a single good reason for VBA arrays to ever go above 4 dimensions. And it literally doesn't have enough memory to create an array of that size anyway.

To reiterate: You shouldn't be doing this in the first place, but if you're going to, for gods' sake get a proper database.

As for your code itself:

I would discuss your confusing and unclear variable naming, but I've already reviewed it twice before and I would just be repeating myself here.

Though I will say: It's been 3 iterations and you're still using s and d as variable names for Data Arrays. Fix it.

Context

StackExchange Code Review Q#114411, answer score: 15

Revisions (0)

No revisions yet.