soonyee91 Posted July 26, 2013 Share Posted July 26, 2013 I wonder is there any function to sort files in array not in certain order but define by ourself? I have no idea on how to start the code structure and any hints are welcome...XD What I want to do is: I have specific file names such as summary, schedule1 and template2 I would like to sort them according to my preference in the array such as: 1. template2 2. schedule1 3. summary I want to sort the files at my will instead of using _arraysort() function. What I mean is I want to set certain files to be always on the 1st list or any files to be specific list. Is there a way to do so? Is my explanation clear? Link to comment Share on other sites More sharing options...
Geir1983 Posted July 26, 2013 Share Posted July 26, 2013 guess you can create it using bubble sort? http://en.wikipedia.org/wiki/Bubble_sort Link to comment Share on other sites More sharing options...
Malkey Posted July 26, 2013 Share Posted July 26, 2013 The example> here sorts an array or string in a specific order. Link to comment Share on other sites More sharing options...
soonyee91 Posted July 26, 2013 Author Share Posted July 26, 2013 (edited) thx for the hints Malkey and Geir1983, But my original intention is: I will select multiple files using _FileOpenDialogArray() (This function will return an array of file names) Func _FileOpenDialogArray($sTitle, $sInitDir, $sFilter, $nOptions = Default, $sDefaultName = '') Local $sDialog = FileOpenDialog($sTitle, $sInitDir, $sFilter, $nOptions, $sDefaultName) If @error Then Return SetError(1, 0, '') If StringInStr($sDialog, '|') Then Local $aSplit = StringSplit($sDialog, '|'), $sHold = '' For $iCC = 2 To UBound($aSplit) - 1 $sHold &= $aSplit[1] & '\' & $aSplit[$iCC] & Chr(1) Next Return StringSplit(StringTrimRight($sHold, 1), Chr(1)) EndIf Local $aArray[2] = [1, $sDialog] Return $aArray EndFunc ;==>_FileOpenDialogArray With all the file names stored in the array variable, it will automatically arrange according to system sorting (I assume is A-Z order??) So When I used the variable later with another function I cannot open the specific files I want, instead follow the system sorting order. Is there a way to rearrange the the file names with my preference? Edited July 26, 2013 by soonyee91 Link to comment Share on other sites More sharing options...
JohnOne Posted July 26, 2013 Share Posted July 26, 2013 Of course there is, but only you know what order you want, so you have to create your own _SortArray() function with your own logic. Look at _ArraySort() for example of looping through logic, but just apply your own. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
UEZ Posted July 26, 2013 Share Posted July 26, 2013 (edited) Maybe this helps, too: >_ArraySort_MultiColumn Br,UEZ Edited July 26, 2013 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
soonyee91 Posted July 26, 2013 Author Share Posted July 26, 2013 thanks JohnOne, I get your point. But the default _arraysort function help did not help much for me to start the function with my own logic or should I follow what previous post on how they sort their function? Basically my logic is simple. I just want sort my files with certain name. for example... I want files contain the term "summary" on the 1st, template on the 2nd and so on... Anyway thanks for the hint... I will try to figure how to start with my sort logic function. Link to comment Share on other sites More sharing options...
Solution JohnOne Posted July 26, 2013 Solution Share Posted July 26, 2013 (edited) Here is a small 1D version to get some ideas from. #include <Array.au3> $sFilenames = "File_with_template.ext,File_with_blah.ext,File_with_summary.ext" $aFilenames = StringSplit($sFilenames, ",", 2) _ArrayDisplay($aFilenames) _SortArray($aFilenames) _ArrayDisplay($aFilenames) Func _SortArray(ByRef $aArray) Local $aTemp = $aArray Local $aMatch[3] = ["summary","template","blah"] Local $iFileNameCount = UBound($aTemp) Local $iReplaceCount = 0 ;loop though all match words For $iMatch = 0 To UBound($aMatch) -1 ; 0 to 2 in this case ConsoleWrite("-Checking matchword " & $aMatch[$iMatch] & @LF) ;loop through file names For $iFilenames = 0 To UBound($aTemp) -1 ; 0 to 2 in this case also If StringInStr($aTemp[$iFilenames], $aMatch[$iMatch]) And $iFileNameCount >= 0 Then $aArray[$iReplaceCount] = $aTemp[$iFilenames] $iReplaceCount += 1 $iFileNameCount -= 1 ConsoleWrite("+Match found in " & $aTemp[$iFilenames] & @LF) ContinueLoop EndIf ConsoleWrite("!No match found in " & $aTemp[$iFilenames] & @LF) Next Next EndFunc Edited July 26, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
soonyee91 Posted July 27, 2013 Author Share Posted July 27, 2013 Thanks JohnOne!!! A great idea for me to start the coding.... I will report if I found problem again....XD Basically your code is the exactly my logic idea.... thanks working on it now. Link to comment Share on other sites More sharing options...
soonyee91 Posted July 27, 2013 Author Share Posted July 27, 2013 JohnOne, I'm actually understand your code well. But how should I use your code? What I mean is the array of file names return using Func _FileOpenDialogArray() include the number of files at the array[0]("first element"). How can I start to sort the array at array[1] instead at the position array[0]? Link to comment Share on other sites More sharing options...
MilesAhead Posted July 27, 2013 Share Posted July 27, 2013 In C/C++ based languages it's old hat to pass in a user-define comparison function to QSort. >This adapted the idea for AutoIt. Before I found that example code I did one myself just passing in a param for the name of the comparison function to accomplish the same thing as a function pointer does in C. I'll defer to the older code. My Freeware Page Link to comment Share on other sites More sharing options...
JohnOne Posted July 27, 2013 Share Posted July 27, 2013 JohnOne, I'm actually understand your code well. But how should I use your code? What I mean is the array of file names return using Func _FileOpenDialogArray() include the number of files at the array[0]("first element"). How can I start to sort the array at array[1] instead at the position array[0]? There are many ways, first that comes to mind is to delete the index that hols the count element in your returned array. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
soonyee91 Posted July 27, 2013 Author Share Posted July 27, 2013 thx JohnOne & MilesAhead, Both of you give me a great idea to deal with my problem. Once again the problem is solved! Link to comment Share on other sites More sharing options...
Belini Posted July 28, 2013 Share Posted July 28, 2013 If the first word is pronounced not be in order. #include <Array.au3> $sFilenames = "indo embora,Tudo bem,tempo perdido,qual lugar,á procura,40 graus,Você chegou,é mesmo,Máquina do tempo,só quero,tá estressado" $aFilenames = StringSplit($sFilenames, ",", 2) _ArrayDisplay($aFilenames) _SortArray($aFilenames) _ArrayDisplay($aFilenames) Func _SortArray(ByRef $aArray) Local $aTemp = $aArray Local $aMatch[3] = ["summary","template","blah"] Local $iFileNameCount = UBound($aTemp) Local $iReplaceCount = 0 ;loop though all match words For $iMatch = 0 To UBound($aMatch) -1 ; 0 to 2 in this case ConsoleWrite("-Checking matchword " & $aMatch[$iMatch] & @LF) ;loop through file names For $iFilenames = 0 To UBound($aTemp) -1 ; 0 to 2 in this case also If StringInStr($aTemp[$iFilenames], $aMatch[$iMatch]) And $iFileNameCount >= 0 Then $aArray[$iReplaceCount] = $aTemp[$iFilenames] $iReplaceCount += 1 $iFileNameCount -= 1 ConsoleWrite("+Match found in " & $aTemp[$iFilenames] & @LF) ContinueLoop EndIf ConsoleWrite("!No match found in " & $aTemp[$iFilenames] & @LF) Next Next EndFunc My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ 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