Jump to content

Sort Array with specific order?


Go to solution Solved by JohnOne,

Recommended Posts

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

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
Link to comment
Share on other sites

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

Maybe this helps, too:  >_ArraySort_MultiColumn

 

Br,

UEZ

Edited 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

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

  • Solution

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.

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

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
Link to comment
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
 Share

  • Recently Browsing   0 members

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