Jump to content

Recommended Posts

Posted

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?

 

Posted (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 by soonyee91
Posted

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.

  • Solution
Posted (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 by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted

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. :thumbsup:

Posted

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]?

Posted

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. :)

Posted

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.

Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...