gcue Posted January 26, 2019 Posted January 26, 2019 hello world i am using UEZ's function to sort arrays on multiple columns. one thing i think it is lacking however is the option to choose at which index to start at. sometimes arrays start at 0 but sometimes they start at 1. headings are sometimes at 0 which throws the sort off quite a bit. any chance this function can be modified to add the option to select which index to start at? hope its easy. expandcollapse popup#include <array.au3> $max = 50 $s = "" Dim $array[$max][4] for $i = 0 to $max - 1 $array[$i][0] = Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) For $j = 0 To 2 If Random(0, 1, 1) Then $s &= Chr(Random(48, 57, 1)) Next If $s = "" Then $s = Chr(Random(65, 67, 1)) $array[$i][1] = $s $array[$i][2] = Random(1, 10, 1) $m = Random(1, 12, 1) $d = Random(1, 31, 1) If $m < 10 Then $m = "0" & $m If $d < 10 Then $d = "0" & $d $array[$i][3] = Random(1900, 2010, 1) & "-" & $m & "-" & $d $s = "" Next _ArrayDisplay($array, 'Before:') Dim $aI[3] ;order of sorting rows - here row 0,3,2 $aI[0] = 0 $aI[1] = 3 $aI[2] = 2 _ArraySort_MultiColumn($array, $aI) _ArrayDisplay($array, 'After:') ; #FUNCTION# ============================================================================= ; Name.............: _ArraySort_MultiColumn ; Description ...: sorts an array at given colums (multi colum sort) ; Syntax...........: _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices) ; Parameters ...: $aSort - array to sort ; $aIndices - array with colum indices which should be sorted in specified order - zero based ; $oDir/$iDir - sort direction - if set to 1, sort descendingly else ascendingly ; Author .........: UEZ ; Version ........: v0.70 build 2013-11-20 Beta ; ========================================================================================= Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0) If Not IsArray($aIndices) Or Not IsArray($aSort) Then Return SetError(1, 0, 0) ;checks if $aIndices is an array If UBound($aIndices) > UBound($aSort, 2) Then Return SetError(2, 0, 0) ;check if $aIndices array is greater the $aSort array Local $1st, $2nd, $x, $j, $k, $l = 0 For $x = 0 To UBound($aIndices) - 1 ;check if array content makes sense If Not IsInt($aIndices[$x]) Then Return SetError(3, 0, 0) ;array content is not numeric Next If UBound($aIndices) = 1 Then Return _ArraySort($aSort, $oDir, 0, 0, $aIndices[0]) ;check if only one index is given _ArraySort($aSort, $oDir, 0, 0, $aIndices[0]) Do $1st = $aIndices[$l] $2nd = $aIndices[$l + 1] $j = 0 $k = 1 While $k < UBound($aSort) If $aSort[$j][$1st] <> $aSort[$k][$1st] Then If $k - $j > 1 Then _ArraySort($aSort, $iDir , $j, $k - 1, $2nd) $j = $k Else $j = $k EndIf EndIf $k += 1 WEnd If $k - $j > 1 Then _ArraySort($aSort, $iDir, $j, $k, $2nd) $l += 1 Until $l = UBound($aIndices) - 1 Return 1 EndFunc thanks for your help
Subz Posted January 26, 2019 Posted January 26, 2019 Maybe something like Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0, $iBase = 0) If $iBase > 0 Then _ArrayDelete($aSort, 0) .... ... .. . If $iBase > 0 Then _ArrayInsert($aSort, 0, UBound($aSort) - 1) Return 1 EndFunc
Nine Posted January 26, 2019 Posted January 26, 2019 You could simple save row 0 then use _ArrayDelete (...) to remove the row. do your multi cols sort. and use _ArrayInsert (...) to put back row 0 “They did not know it was impossible, so they did it” ― Mark Twain Reveal hidden contents Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
jchd Posted January 26, 2019 Posted January 26, 2019 We definitely need an _ArrayQSort taking a collation function as parameter, just like qsort() in C. I'll make one some day if it doesn't exist yet somewhere. Reveal hidden contents This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
gcue Posted January 26, 2019 Author Posted January 26, 2019 (edited) On 1/26/2019 at 3:22 PM, Subz said: Maybe something like Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0, $iBase = 0) If $iBase > 0 Then _ArrayDelete($aSort, 0) .... ... .. . If $iBase > 0 Then _ArrayInsert($aSort, 0, UBound($aSort) - 1) Return 1 EndFunc Expand thanks that worked! however there's something weird going on - when specifying a base I would have to include specifying the other options before it $oDir = 0, $iDir = 0 when i specify this it doesnt work Quote Local $sort_by_columns[2] = [1, 0] _ArraySort_MultiColumn($post_array, $sort_by_columns, 0, 0, 0) Expand however when i specify like this it works Quote Local $sort_by_columns[2] = [1, 0] _ArraySort_MultiColumn($post_array, $sort_by_columns, 0) Expand Edited January 26, 2019 by gcue
gcue Posted January 26, 2019 Author Posted January 26, 2019 On 1/26/2019 at 3:39 PM, jchd said: We definitely need an _ArrayQSort taking a collation function as parameter, just like qsort() in C. I'll make one some day if it doesn't exist yet somewhere. Expand yea surprised it doesnt exist already
UEZ Posted January 26, 2019 Posted January 26, 2019 (edited) Can you test this please? expandcollapse popup#include-once #include <Array.au3> ; #FUNCTION# ============================================================================= ; Name.............: _ArraySort_MultiColumn ; Description ...: sorts an array at given colums (multi colum sort) ; Syntax...........: _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices) ; Parameters ...: $aSort - array to sort ; $aIndices - array with colum indices which should be sorted in specified order - zero based ; $oDir/$iDir - sort direction - if set to 1, sort descending else ascending. $oDir is for the 1st row, ; $iDir for the remaining rows ; $iStart - where to start to sort - 0 = 0 based array, 1 = 1 based array ; Author .........: UEZ ; Version ........: v0.80 build 2019-01-26 Beta ; ========================================================================================= Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0, $iStart = 0) If Not IsArray($aIndices) Or Not IsArray($aSort) Then Return SetError(1, 0, 0) ;checks if $aIndices is an array If UBound($aSort, 2) = 0 Then Return SetError(4, 0, 0) ;array is 1D only If UBound($aIndices) > UBound($aSort, 2) Then Return SetError(2, 0, 0) ;check if $aIndices array is greater the $aSort array Local $1st, $2nd, $x, $j, $k, $l = 0 For $x = 0 To UBound($aIndices) - 1 ;check if array content makes sense If Not IsInt($aIndices[$x]) Then Return SetError(3, 0, 0) ;array content is not numeric Next $iStart = Int($iStart) < 0 ? 0 : Int($iStart) > 1 ? 1 : Int($iStart) If UBound($aIndices) = 1 Then Return _ArraySort($aSort, $oDir, $iStart, 0, $aIndices[0]) ;check if only one index is given _ArraySort($aSort, $oDir, $iStart, 0, $aIndices[0]) Do $1st = $aIndices[$l] $2nd = $aIndices[$l + 1] $j = 0 $k = 1 While $k < UBound($aSort) If $aSort[$j][$1st] <> $aSort[$k][$1st] Then If $k - $j > 1 Then _ArraySort($aSort, $iDir, $j, $k - 1, $2nd) $j = $k Else $j = $k EndIf EndIf $k += 1 WEnd If $k - $j > 1 Then _ArraySort($aSort, $iDir, $j, $k, $2nd) $l += 1 Until $l = UBound($aIndices) - 1 Return 1 EndFunc ;==>_ArraySort_MultiColumn Seems to work but I'm not 100% sure... Edited January 26, 2019 by 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
gcue Posted January 26, 2019 Author Posted January 26, 2019 (edited) not quite. i tried a base 1 index array (specified it to start at 1) and it started with 0 anyway. my array has 6 columns - maybe thats why? syntax used Local $sort_by_columns[2] = [6, 1] _ArraySort_MultiColumn($final_array, $sort_by_columns, 1, 1) Edited January 26, 2019 by gcue
UEZ Posted January 26, 2019 Posted January 26, 2019 The 5th parameter is for setting the search function to base 0 or base 1. In your example you will sort the array descending but not 1 based. Try _ArraySort_MultiColumn($final_array, $sort_by_columns, 1, 1, 1) 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
gcue Posted January 26, 2019 Author Posted January 26, 2019 works! the parameters in your description i think are confusing ; Parameters ...: $aSort - array to sort ; $aIndices - array with colum indices which should be sorted in specified order - zero based ; $oDir/$iDir - sort direction - if set to 1, sort descendingly else ascendingly ; $iStart - where to start to sort - 0 = 0 based array, 1 = 1 based array those are 4 parameters but the function is accepting 5 i think the confusion lies with $oDir/$iDir - sort direction - if set to 1, sort descendingly else ascendingly is that 1 or 2 variables? i think its 2 seperate ones from what the function is accepting anyway thank you so much for your help!
UEZ Posted January 26, 2019 Posted January 26, 2019 Yes, it is a little bit confusing. $oDir is for the 1st column and $iDir for the rest. 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Moderators Melba23 Posted January 26, 2019 Moderators Posted January 26, 2019 gcue, Or you could use my ArrayMultiColSort UDF, which has the start and finish position of the sort already built-in: #include "ArrayMultiColSort.au3" $max = 50 $s = "" Dim $array[$max][4] for $i = 0 to $max - 1 $array[$i][0] = Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) & Chr(Random(65, 67, 1)) For $j = 0 To 2 If Random(0, 1, 1) Then $s &= Chr(Random(48, 57, 1)) Next If $s = "" Then $s = Chr(Random(65, 67, 1)) $array[$i][1] = $s $array[$i][2] = Random(1, 10, 1) $m = Random(1, 12, 1) $d = Random(1, 31, 1) If $m < 10 Then $m = "0" & $m If $d < 10 Then $d = "0" & $d $array[$i][3] = Random(1900, 2010, 1) & "-" & $m & "-" & $d $s = "" Next _ArrayDisplay($array, 'Before:') ; Set the order in which the columns are to be sorted and how to sort them ; First we sort col 0, then col 3, and finally col 2 - all in ascending order Global $aSortData[][] = [[0, 0], [3, 0], [2, 0]] ; Sort the array, starting at row 1 _ArrayMultiColSort($array, $aSortData, 1) _ArrayDisplay($array, 'After:') M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Reveal hidden contents ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now