VBA sort multiple columns and multiple keys array

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
I'm looking for a function that can sort multiple columns and multiple keys array, and it should be dynamic even on even at the expense of performance.

This function should have 3 parameters:

  1. The 2 dimensions array we want to sort.
  2. An array of the "columns key" numbers we want to sort.
  3. An boolean array that True = Ascending and False = Descending
In the end this function should return the sorted array.

For example:

Dim SortedArray as Variant

SortedArray = MySuperSortArrayFnc(MyArr, Array(4,1,7), Array(True, False, False))

MyArr = My 2 dimensions source array that i want to sort.

Array(4,1,7) = the order/levels of the sorting.

Array(True, False, False) = it should be parallel to the previous array parameter, so column 4 is ascending, column 1 is descending and column 7 is descending.

Notes:

  1. The function cant allow to use a worksheet, it should be pure VBA logic.
  2. No need to handle a single array column scenario, only multiple columns array.
  3. No need to handle validation on the input(parameters) like if the "MyArr" paramter is not empty.
  4. Empty values or any other value should be sort order like the excel sort order, if the excel send the empty value to bottom, its fine by me.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top