Jump to content

Recommended Posts

Posted (edited)

Hi,

I'd like to ask for a little assistance again, but this time related to array sorting.
I have the following code:

Global $sSourceCSV
Global $sCSVContent

DeleteCSVEOLCommas()

Func DeleteCSVEOLCommas() ;Deletes end of line commas from source .csv file
    Local $aArray = FileReadToArray($sSourceCSV)
        For $i = 0 To UBound($aArray) - 1
            $sCSVContent &= StringTrimRight($aArray[$i], 1) & @LF
        Next
    Return $sCSVContent
EndFunc   ;==>DeleteCSVEOLCommas

And the content of $sSourceCSV:

Instrument,Type,Asset,Order #,Order Time,Invested,Target Price,Expiry Price,Expiry Time,Return,
High / Low,Low,AUD/USD,36007202,02-09-15 17:51:06,24.00,0.70351,0.70414,02-09-15 18:30:00,0.00,
High / Low,Low,AUD/USD,36007200,01-09-15 17:45:03,24.00,0.70350,0.70411,02-09-15 18:30:00,0.00,
High / Low,Low,NZD/USD,36004443,02-09-15 16:53:52,24.00,0.63465,0.63534,02-09-15 17:30:00,0.00,
High / Low,Low,AUD/USD,36004413,02-09-15 16:53:20,24.00,0.70238,0.70347,02-09-15 17:30:00,0.00,

This script removes the commas at the end of every line and adds a line feed - this is necessary to be able to process it further with an external application and it's working as intended so far :lol:

What I'd like to achieve is to sort the lines first according to the "Expiry Time" field (ascending) and then by the "Order Time" field (also ascending). So the result would be this:

Instrument,Type,Asset,Order #,Order Time,Invested,Target Price,Expiry Price,Expiry Time,Return
High / Low,Low,AUD/USD,36004413,02-09-15 16:53:20,24.00,0.70238,0.70347,02-09-15 17:30:00,0.00
High / Low,Low,NZD/USD,36004443,02-09-15 16:53:52,24.00,0.63465,0.63534,02-09-15 17:30:00,0.00
High / Low,Low,AUD/USD,36007200,01-09-15 17:45:03,24.00,0.70350,0.70411,02-09-15 18:30:00,0.00
High / Low,Low,AUD/USD,36007202,02-09-15 17:51:06,24.00,0.70351,0.70414,02-09-15 18:30:00,0.00

The function should be extended with this part so the returned $sCSVContent var would contain the result. Any ideas are welcome and appreciated ^_^

Edited by NoizeBit
Posted (edited)

You could do something like this. Make sure you check out all the functions in this script using the help file.

#include <Array.au3>
#include <File.au3>

Local $aRetArray

; read csv to an array
_FileReadToArray('my.csv', $aRetArray, $FRTA_NOCOUNT, ',')
;_ArrayDisplay($aRetArray)

; get rid of the extra column
ReDim $aRetArray [UBound($aRetArray)][UBound($aRetArray, 2) -1]
_ArrayDisplay($aRetArray)

; sort by order time
_ArraySort ($aRetArray , 0, 1, 0 ,4)
_ArrayDisplay($aRetArray)

 

Edited by czardas
  • Moderators
Posted

NoizeBit,

You could use my ArrayMultiColSort UDF to do this easily - but you will need to swap the column order as the UDF only sorts them in left-to-right order:

#include <Array.au3>
#include <File.au3>
#include "ArrayMultiColSort.au3"

$sFile = "test.csv"

; Read file into a 2D array
Global $aArray

_FileReadToArray($sFile, $aArray, $FRTA_NOCOUNT, ",")

; And here it is
_ArrayDisplay($aArray, "Original", Default, 8)

; Swapt the columns to get them in the right order
_ArraySwap($aArray, 4, 8, True)
_ArrayDisplay($aArray, "Swapped", Default, 8)

; Sort in the required order
Local $aSortData[2][2] = [[4, 0], [8, 0]]
_ArrayMultiColSort($aArray, $aSortData, 1)

; And her is the result
_ArrayDisplay($aArray, "Sorted", Default, 8)

; Finally swap the columns back
_ArraySwap($aArray, 4, 8, True)
 _ArrayDisplay($aArray, "Swapped back", Default, 8)

Please ask if you have any questions.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted
  On 9/14/2015 at 1:35 PM, Melba23 said:

NoizeBit,

You could use my ArrayMultiColSort UDF to do this easily - but you will need to swap the column order as the UDF only sorts them in left-to-right order:

#include <Array.au3>
#include <File.au3>
#include "ArrayMultiColSort.au3"

$sFile = "test.csv"

; Read file into a 2D array
Global $aArray

_FileReadToArray($sFile, $aArray, $FRTA_NOCOUNT, ",")

; And here it is
_ArrayDisplay($aArray, "Original", Default, 8)

; Swapt the columns to get them in the right order
_ArraySwap($aArray, 4, 8, True)
_ArrayDisplay($aArray, "Swapped", Default, 8)

; Sort in the required order
Local $aSortData[2][2] = [[4, 0], [8, 0]]
_ArrayMultiColSort($aArray, $aSortData, 1)

; And her is the result
_ArrayDisplay($aArray, "Sorted", Default, 8)

; Finally swap the columns back
_ArraySwap($aArray, 4, 8, True)
 _ArrayDisplay($aArray, "Swapped back", Default, 8)

Please ask if you have any questions.

M23

Hi Melba,

Thanks a lot, just when I thought I'd have to automate a csv editor to do the sorting you've supplied the best solution ;) Great work!

  • Moderators
Posted

NoizeBit,

Glad I could help.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • Moderators
Posted

NoizeBit,

Slight change to the UDF and it now sorts without having to swap the columns - the new files are in the UDF thread.

#include <Array.au3>
#include "ArrayMultiColSort.au3"

Global $aArray[][]  = [ _
["Instrument", "Type", "Asset",   "Order #", "Order Time",        "Invested", "Target Price", "Expiry Price", "Expiry Time",        "Return"], _
["High / Low", "Low",  "AUD/USD", "36007202", "02-09-15 17:51:06", "24.00",    "0.70351",      "0.70414",      "02-09-15 18:30:00", "0.00"], _
["High / Low", "Low",  "AUD/USD", "36007200", "01-09-15 17:45:03", "24.00",    "0.70350",      "0.70411",      "02-09-15 18:30:00", "0.00"], _
["High / Low", "Low",  "NZD/USD", "36004443", "02-09-15 16:53:52", "24.00",    "0.63465",      "0.63534",      "02-09-15 17:30:00", "0.00"], _
["High / Low", "Low",  "AUD/USD", "36004413", "02-09-15 16:53:20", "24.00",    "0.70238",      "0.70347",      "02-09-15 17:30:00", "0.00"]]

_ArrayDisplay($aArray, "Original", Default, 8)

; Sort in the required order
Local $aSortData[2][2] = [[8, 0], [4, 0]]
_ArrayMultiColSort($aArray, $aSortData, 1)

; And here is the result
_ArrayDisplay($aArray, "Sorted", Default, 8)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)
  On 9/14/2015 at 3:59 PM, Melba23 said:

NoizeBit,

Glad I could help.

M23

Just one thing: how do I return the result to a variable? If I do so it returns only 0, but how do I return the sorted content?

#include <Array.au3>
#include <File.au3>
#include <ArrayMultiColSort.au3>

Global $sSourceCSV = @ScriptDir & "\test.csv"
Global $sCSVContentSorted

SortCSVLines()

MsgBox(0, "", $sCSVContentSorted)

Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    Local $aArray
        _FileReadToArray($sSourceCSV, $aArray, $FRTA_NOCOUNT, ",")
        Local $aSortData[2][2] = [[4, 0], [8, 0]]
        $sCSVContentSorted = _ArrayMultiColSort($aArray, $aSortData, 1)
    Return $sCSVContentSorted
EndFunc   ;==>SortCSVLines

 

Edited by NoizeBit
Posted
  On 9/15/2015 at 10:01 AM, Melba23 said:

NoizeBit,

Slight change to the UDF and it now sorts without having to swap the columns - the new files are in the UDF thread.

#include <Array.au3>
#include "ArrayMultiColSort.au3"

Global $aArray[][]  = [ _
["Instrument", "Type", "Asset",   "Order #", "Order Time",        "Invested", "Target Price", "Expiry Price", "Expiry Time",        "Return"], _
["High / Low", "Low",  "AUD/USD", "36007202", "02-09-15 17:51:06", "24.00",    "0.70351",      "0.70414",      "02-09-15 18:30:00", "0.00"], _
["High / Low", "Low",  "AUD/USD", "36007200", "01-09-15 17:45:03", "24.00",    "0.70350",      "0.70411",      "02-09-15 18:30:00", "0.00"], _
["High / Low", "Low",  "NZD/USD", "36004443", "02-09-15 16:53:52", "24.00",    "0.63465",      "0.63534",      "02-09-15 17:30:00", "0.00"], _
["High / Low", "Low",  "AUD/USD", "36004413", "02-09-15 16:53:20", "24.00",    "0.70238",      "0.70347",      "02-09-15 17:30:00", "0.00"]]

_ArrayDisplay($aArray, "Original", Default, 8)

; Sort in the required order
Local $aSortData[2][2] = [[8, 0], [4, 0]]
_ArrayMultiColSort($aArray, $aSortData, 1)

; And here is the result
_ArrayDisplay($aArray, "Sorted", Default, 8)

M23

Thanks Melba, I'll update my script accordingly.

Posted (edited)

I'm trying to put yor UDF inside a function, but I can't figure out why does it return the original data instead of the sorted result?

#include <Array.au3>
#include <File.au3>
#include <ArrayMultiColSort.au3>

Global $sSourceCSV = @ScriptDir & "\test.csv"
Global $aArray, $sCSVContentSorted

SortCSVLines()

MsgBox(0, "", $sCSVContentSorted)

Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    _FileReadToArray($sSourceCSV, $aArray, ",")
        For $i = 0 To UBound($aArray) - 1
            Local $aSortData[2][2] = [[4, 0], [8, 0]]
            _ArrayMultiColSort($aArray, $aSortData, 1)
            $sCSVContentSorted &= $aArray[$i] & @LF
        Next
    Return $sCSVContentSorted
EndFunc   ;==>SortCSVLines
Edited by NoizeBit
Posted (edited)

Does this do what you want?

#include <Array.au3>
#include <File.au3>
#include <ArrayMultiColSort.au3>

Global $sSourceCSV = @ScriptDir & "\test.csv"
Global $sCSVContentSorted

SortCSVLines()
MsgBox(0, "", $sCSVContentSorted)


Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    Local $aArray
    _FileReadToArray($sSourceCSV, $aArray, $FRTA_NOCOUNT, ",")

    Local $aSortData[2][2] = [[4, 0], [8, 0]]
    _ArrayMultiColSort($aArray, $aSortData, 1)

    For $i = 0 To UBound($aArray) - 1 ; first loop
        For $j = 0 To UBound($aArray, 2) - 1 ; second loop
            $sCSVContentSorted &= $aArray[$i][$j] & ","
        Next
        $sCSVContentSorted = StringTrimRight($sCSVContentSorted, 1)
        $sCSVContentSorted &= @LF
    Next

    ; There is no need to return a global variable because it has been modified in the global scope
    ;Return $sCSVContentSorted
EndFunc   ;==>SortCSVLines

Edit - I forgot to change the csv file name back to the original: try it now.
Edit2- Removed $aArray to local scope where it belongs and fixed a small problem with the code.

You might want to replace @LF with @CRLF

Edited by czardas
Posted
  On 9/17/2015 at 5:25 PM, czardas said:

Does this do what you want?

#include <Array.au3>
#include <File.au3>
#include <ArrayMultiColSort.au3>

Global $sSourceCSV = @ScriptDir & "\test.csv"
Global $sCSVContentSorted

SortCSVLines()
MsgBox(0, "", $sCSVContentSorted)


Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    Local $aArray
    _FileReadToArray($sSourceCSV, $aArray, $FRTA_NOCOUNT, ",")
    
    Local $aSortData[2][2] = [[4, 0], [8, 0]]
    _ArrayMultiColSort($aArray, $aSortData, 1)

    For $i = 0 To UBound($aArray) - 1
        For $j = 0 To UBound($aArray, 2) - 1
            $sCSVContentSorted &= $aArray[$i][$j] & ","
        Next
        StringTrimRight($sCSVContentSorted, 1)
        $sCSVContentSorted &= @LF
    Next

    ; There is no need to return a global variable because it has been modified in the global scope
    ;Return $sCSVContentSorted
EndFunc   ;==>SortCSVLines

Edit - I forgot to change the csv file name back to the original: try it now.
Edit2- Removed $aArray to local scope where it belongs.

Thanks for the fast reply, czardas! I see now what was missing, thanks for the contribution ;)

  • Moderators
Posted

NoizeBit,

Are you all set now? This works for me:

#include <Array.au3>
#include <File.au3>
#include "M:\Program\Au3 Scripts\UDFs\ArrayMultiColSort\ArrayMultiColSort.au3"

Global $aArray



SortCSVLines()

_ArrayDisplay($aArray, "", Default, 8)

Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    ; Simulate reading file
    Global $aArray[][]  = [ _
    ["Instrument", "Type", "Asset",   "Order #", "Order Time",        "Invested", "Target Price", "Expiry Price", "Expiry Time",        "Return"], _
    ["High / Low", "Low",  "AUD/USD", "36007202", "02-09-15 17:51:06", "24.00",    "0.70351",      "0.70414",      "02-09-15 18:30:00", "0.00"], _
    ["High / Low", "Low",  "AUD/USD", "36007200", "01-09-15 17:45:03", "24.00",    "0.70350",      "0.70411",      "02-09-15 18:30:00", "0.00"], _
    ["High / Low", "Low",  "NZD/USD", "36004443", "02-09-15 16:53:52", "24.00",    "0.63465",      "0.63534",      "02-09-15 17:30:00", "0.00"], _
    ["High / Low", "Low",  "AUD/USD", "36004413", "02-09-15 16:53:20", "24.00",    "0.70238",      "0.70347",      "02-09-15 17:30:00", "0.00"]]
    ; Set sort order
    Local $aSortData[2][2] = [[8, 0], [4, 0]]
    ; Sort array
    _ArrayMultiColSort($aArray, $aSortData, 1)
EndFunc   ;==>SortCSVLines

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted
  On 9/17/2015 at 5:46 PM, czardas said:

No worries. You will notice I overlooked a couple of things in my first post. I'm a bit hung over - that's my excuse. ^^ Grab the modified code above.

Got it, I'll put it to good use :lol:

Posted
  On 9/17/2015 at 5:54 PM, Melba23 said:

NoizeBit,

Are you all set now? This works for me:

#include <Array.au3>
#include <File.au3>
#include "M:\Program\Au3 Scripts\UDFs\ArrayMultiColSort\ArrayMultiColSort.au3"

Global $aArray



SortCSVLines()

_ArrayDisplay($aArray, "", Default, 8)

Func SortCSVLines() ;Sorts lines first by Expiry Time and then by Order Time
    ; Simulate reading file
    Global $aArray[][]  = [ _
    ["Instrument", "Type", "Asset",   "Order #", "Order Time",        "Invested", "Target Price", "Expiry Price", "Expiry Time",        "Return"], _
    ["High / Low", "Low",  "AUD/USD", "36007202", "02-09-15 17:51:06", "24.00",    "0.70351",      "0.70414",      "02-09-15 18:30:00", "0.00"], _
    ["High / Low", "Low",  "AUD/USD", "36007200", "01-09-15 17:45:03", "24.00",    "0.70350",      "0.70411",      "02-09-15 18:30:00", "0.00"], _
    ["High / Low", "Low",  "NZD/USD", "36004443", "02-09-15 16:53:52", "24.00",    "0.63465",      "0.63534",      "02-09-15 17:30:00", "0.00"], _
    ["High / Low", "Low",  "AUD/USD", "36004413", "02-09-15 16:53:20", "24.00",    "0.70238",      "0.70347",      "02-09-15 17:30:00", "0.00"]]
    ; Set sort order
    Local $aSortData[2][2] = [[8, 0], [4, 0]]
    ; Sort array
    _ArrayMultiColSort($aArray, $aSortData, 1)
EndFunc   ;==>SortCSVLines

M23

Yeah, only the returning part was a bit cloudy for me, but now it produces the exact result I was looking for, thanks.

  • Moderators
Posted

NoizeBit,

Excellent.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted
  On 9/17/2015 at 6:09 PM, czardas said:

It's Melba who did all the hard work really. It's good you tried to figure it out yourself.

I can't argue with that, but I thank both of you for pushing me toward the right direction B)

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