Sign in to follow this  
Followers 0
soonyee91

Sort Array with specific order?

14 posts in this topic

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?

 

Share this post


Link to post
Share on other sites



The example> here sorts an array or string in a specific order.

Share this post


Link to post
Share on other sites

#4 ·  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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#8 ·  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.

Share this post


Link to post
Share on other sites

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:

Share this post


Link to post
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]?

Share this post


Link to post
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. :)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

thx JohnOne & MilesAhead,

Both of you give me a great idea to deal with my problem. Once again the problem is solved! :thumbsup:

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0