Sign in to follow this  
Followers 0
Thopaga

Sort multiple or multi-dimensional arrays with multiple sort columns.

5 posts in this topic

Sort multiple or multi-dimensional arrays with multiple sort columns.

Sort two arrays with the same dimensions at once or a multi-dimensional array by one or more dimensions.

When multiple arrays are used only the first array has parameters, so that the second one will be

sorted in the same way as the first one. Good when you need a user exit to sort an array in a special way.

This sort routine focus on functionality and flexibility and not on performance.

It is meant to be a supplement to the _ArraySort routine which focus is on performance.

I have included a parameter description below which gives you an idea how it works.

Also I feel that the example file should get you started.

Thanks to the many clever members and kind helpers on this forum who inspired me to write this.

I am really impressed by the many pioneers who have made such very good UDFs and routines.

Suggestion: A category listing for all those unofficial routines that has been made with link to the example forum.

The person itself could put it into that category and remove it, so that little administration is necessary.

Good luck to you.

; #FUNCTION# ====================================================================================================================
; Name...........: _ArraySortMulti
; Description ...: Sort multiple or multi-dimensional arrays with multiple sort columns.
;                  Sort two arrays with the same dimensions at once or a multi-dimensional array by one or more dimensions.
;                  When multiple arrays are used only the first array has parameters, so that the second one will be
;                  sorted in the same way as the first one. Good when you need a user exit to sort an array in a special way.
; Syntax.........: _ArraySortMulti(ByRef $avArray .., COL0 ..[, mixed $arg], COL1 ..[, mixed $arg], [DEBUG])
; Parameters ....: $avArray       - Arrays to sort separated by comma. Only one or two arrays allowed.
;                                   Multiple arrays must be of the same dimensions, but can contain any data.
;                                   Only the first array in the list will have parameters.
;                                   The second one will be sorted the same way as the first.
;                  $sCOL0,$sCOL1  - Required. Must be COL followed by a number. First column start with zero - COL0.
;                                   A 1-dimensional array has one column named COL0. Multiple sort columns are allowed in any order.
;                                   This parameter cannot be a constant since you can have two array parameters with ByRef.
;                  $arguments     - [optional] ASC, DESC, REGULAR, NUMERIC, STRING, INSEN_STRING, UserOrder, HexOrder.
;                                   Can be repeated for every column to sort. All arguments can be specified in any order
;                                   and in both lower and upper case. You can place all arguments in one string separated by comma
;                                   or mix it with multiple strings in separate variables. Do whatever is convenient to you.
;                  $sASC          - Default=ASC. ASC gives Ascending sort order.
;                  $sDESC         - Default=ASC. DESC gives Descending sort order.
;                  $sREGULAR      - Default=REGULAR. REGULAR treat values as they are. Don't change types.
;                                   Lexicographically string compares. Case insensitive.
;                                   WARNING: Using REGULAR with strings might give you unpredictable results like 'AA' > 'AB'.
;                                   Use STRING, INSEN_STRING and NUMERIC whenever possible.
;                  $sNUMERIC      - Default=REGULAR. NUMERIC treat values numerically.
;                  $sSTRING       - Default=REGULAR. STRING treat values as strings. Case sensitive.
;                  $sINSEN_STRING - Default=REGULAR. INSEN_STRING treat values as strings. Case insensitive.
;                  $sUserOrder    - Default=No order change. Change sort order without changing any data in the array.
;                                   Specify UserOrder=(DataFrom=DataTo in pairs (FromTo) separated by semicolon).
;                                   Example UserOrder=(January=1;February=2;March=3;April=4) Changes the orders of months lacking numbers.
;                                   You do not use single or double quotes inside parenthesis unless they are data themselves.
;                                   Parameters like NUMERIC, STRING applies like normal, see examples.
;                  $sHexOrder     - Default=No order change. Change sort order without changing any data in the array.
;                                   Specify HexOrder=(HexFrom=HexTo in pairs (FromTo) separated by semicolon).
;                                   Example HexOrder=(C6=5B;D8=5C;C5=5D;E6=7B;F8=7C;E5=7D) Changes hexcode C6 to 5B and so on.
;                                   The main purpose of this is to simplify sorting for countries like the Scandinavians
;                                   who use additional letters to the english alphabet. A one time job for each country.
;                                   Example _StringToHex('Æ') & _StringToHex(Chr(91)) vil give HexOrder=(C6=5B)
;                                   Replace hexcode C6 with 5B without changing data so as to change sort order of strings.
;                  $sDEBUG        - Default=No debug. If DEBUG will list the arrays before and after sort. You only need to specify once.
; Return values .: Success = 1
;                  Failure = 0, sets @error:
;                  |1 - $avArray is not an array
;                  |2 - Multiple arrays is not of the same dimensions
;                  |3 - Invalid COL parameter
;                  |4 - COL parameter missing or in wrong position
;                  |5 - Unknown parameter found
;                  |6 - Max 9-dimensions exceeded. Can easily be extended on request.
;                  |7 - Cannot use NUMERIC and HexOrder on the same column
;                  |8 - HexOrder specifications invalid
;                  |9 - UserOrder specifications invalid
; Author ........: Thopaga
; Modified.......:
; Remarks .......: This routine focus on functionality and flexibility and not on performance.
;                  It is meant to be a supplement to the _ArraySort routine which focus is on performance.
; Related .......: _ArraySort
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================

ArraySortMulti.zip

Share this post


Link to post
Share on other sites



Cool, looks very good! :x

Might be useful someday...

Thanks for sharing!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Suggestion: A category listing for all those unofficial routines that has been made with link to the example forum.

The person itself could put it into that category and remove it, so that little administration is necessary.

Although I like the idea in general, I'm a little skeptical about it actually working in the long run. If you could elaborate on it some more that would be nice. (Probably better to do so in a separate chat topic.)

Not looked at your example yet, but it sounds interesting.

TIA for sharing.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Thanks UEZ, thanks MvGulik for your kind comments.

Coming to my suggestion, I have now found out that there exists a category listing under Downloads.

Surprisingly very few use it.

Whatever the reasons, it means that a lot of good unofficial UDFs and routines will remain unnoticed by many.

Thanks.

Share this post


Link to post
Share on other sites

Very useful, thanks a lot for sharing it!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0