Malkey Posted June 3, 2011 Share Posted June 3, 2011 (edited) Inspired by the thread This example sorts characters. The sorting order is specified.In the example:-The sort order used is "bacdefghijnmlopqrstuwxyz0246813579".The test string is "at vase be conker knuckle cool carl collected calm 0 1 2 3 4 5 6 7 8 9 22 23 24 25 26"Note the order "ba". Results in the order "be" then "at".Note the order "nmlo". Results in the order "conker" then "collected", followed by "cool".Note the order of the numbers. The results shows the numbers in the order, "0 2 22 24 26 23 25 4 6 8 1 3 5 7 9".Note in the order "k" and "v" are missing. So, "knuckle" then "vase" is sort at the bottom by _ArraySort.expandcollapse popup#include <Array.au3> ; Example: ; Note the order "ba". Results in the order "be" then "at". ; Note the order "nmlo". Results in the order "conker" then "collected", followed by "cool". ; Note the order of the numbers. The results shows the numbers in that same order. ; Note in the order "k" and "v" are missing. So, "knuckle" then "vase" is sort at the bottom by _ArraySort. Local $sAlphabetOrder = "bacdefghijnmlopqrstuwxyz0246813579" Local $sString = "at vase be conker knuckle cool carl collected calm 0 1 2 3 4 5 6 7 8 9 22 23 24 25 26" Local $aString = StringSplit($sString, " ", 2) ;$aRet = _ArraySortInOrder($aString, $sAlphabetOrder) ; or $aRet = _ArraySortInOrder($sString, $sAlphabetOrder) If Not @error Then _ArrayDisplay($aRet, "Sorted Array") ; Description - Sorts an array or a space separated string in the order of the characters listed in a given string. ; Parameters: ; $aStr - The array, or the space separated string to be sorted. ; $sAlphabetOrder - A string containing the sorting order of the characters which appear in the array or string. ; Requirement: #include <Array.au3> ; Returns: A sorted array. ; Func _ArraySortInOrder($aStr, $sAlphabetOrder) $sAlphabetOrder = StringStripWS($sAlphabetOrder, 8) ; Cannot sort on a space. Local $aAlpha = StringSplit($sAlphabetOrder, "", 2) ;--- Convert the array ($aStr) to a string or convert the string ($aStr) to an array.---- Local $sToSort If IsArray($aStr) Then For $i = 0 To UBound($aStr) - 1 $sToSort &= $aStr[$i] & " " Next $sToSort = StringTrimRight($sToSort, 1) ; Remove trailing space. ElseIf IsString($aStr) Then $sToSort = StringStripWS($aStr, 7) $aStr = StringSplit($sToSort, " ", 2) Else Return SetError(0, 0, 1) EndIf ;--->End of Convert array_string.---- ; Convert all characters in $sToSort to $sAlphabetOrder characters. ; Note; "#>" was randomly chosen to prevent previous converted characters to be converted again. For $i = 0 To UBound($aAlpha) - 1 $sToSort = StringRegExpReplace($sToSort, $aAlpha[$i] & "(?!\d{0,2}#>)", StringRight("00" & $i, 3) & "#>") Next ;Create 2D array with $sAlphabetOrder character words in column 0, and given words in the next column. Local $aToSort = StringSplit($sToSort, " ", 2) Local $aArr[UBound($aStr)][2] For $i = 0 To UBound($aStr) - 1 $aArr[$i][0] = ($aToSort[$i]) $aArr[$i][1] = $aStr[$i] Next ;--->End of Create 2D array ------------------------------------------------------------- _ArraySort($aArr) ; Sort on column 0 ; Copy the 2nd column of the 2D array into a 1D array. For $i = 0 To UBound($aStr) - 1 $aStr[$i] = $aArr[$i][1] Next Return $aStr EndFunc ;==>_ArraySortInOrder Edited June 21, 2012 by Malkey Link to comment Share on other sites More sharing options...
Sateesh Posted April 11, 2014 Share Posted April 11, 2014 (edited) Excellent. I used this script to read data from a file, sort it and save. Saved a lot of time. Edited April 11, 2014 by Sateesh Link to comment Share on other sites More sharing options...
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