Jump to content

Trouble with Array Sort


Go to solution Solved by czardas,

Recommended Posts

The array sort in this code is not working, and I don't understand why at this point.

Here is what I'm doing:

Reading in a CSV file (three pieces of data to each line).

Since each line has a category, I am summing the categories to get a single total for each category.

Then I delete out the "blank" lines, retaining only the lines that have a summed number.

Next is to sort the array in descending order on the "summed" number.

Sorting is a problem. Doesn't do what its is supposed to do.

I have Array Display in the code so you can see what is going on as well.

The summation code came from the forum and works, as you can see.  Whoever I got it from did a nice job. Trying to find in my notes who it was - thanks.

Here is a data set of what I tested with.  It worked with a couple of records but then got jumbled when I added more. Confused on that as well.

"ABC",16.4,"Gas"
"ABC",35.01,"Gas"
"ABC",36.15,"Gas"
"ABC",22.15,"Eat Out"
"ABC",22.65,"Eat Out"
"ABC",8.93,"Eat Out"
"ABC",20.22,"Eat Out"
"ABC",10.01,"Eat Out"
"ABC",20.79,"Eat Out"
"ABC",18.22,"Eat Out"
"ABC",15.18,"Eat Out"
"ABC",80.25,"Food"
"ABC",31.59,"Food"
"ABC",11.75,"Food"
"ABC",12.56,"Food"
"ABC",8.05,"Food"
"ABC",41.66,"Food"
"ABC",46.13,"Food"
"ABC",36.69,"Food"
"ABC",18.05,"Household"
"ABC",16.17,"Misc"
"ABC",74.27,"Xmas"
"ABC",10.82,"Misc"
"ABC",3.57,"Misc"
"ABC",10.37,"Food"
"ABC",16.27,"Eat Out"
"ABC",60,"Vacation"
"ABC",10,"Haircut"

"ABC",10.08,"Clothes"

Thanks for any guidance on this one.

Hobbyist

;<<<<<<<<<<<<<<<<<<<<<<<<
#include <Array.au3>
#include <File.au3>
#include <Misc.au3>
#include <FileConstants.au3>


Global Enum $iValues_Col0, $iValues_Col1, $iValues_Col2, $iValues_Col3, $iValues_Price, $iValues_Type, $iValues_Sum,$iValues_SumLOG
Global Enum $iValues2_ven, $iValues2_price, $iValues2_type,$iValues2_SumLOG



Local $budgetlogfile = "c:\Records\2014 Budget\Monthly Log\September.csv" ;this is where I have the data stored, example below.

$csv = FileRead ($budgetlogfile)
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aLog[$rows[0] + 1][4]                    ;8 for columns adjust
$aLog[2][0] = $rows[0]  ;$aLog[0][0] = $rows[0]
For $i = 1 to $rows[0] ;For $i = 1 to $rows[0]

    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1

        $aLog[$i][$j] = $temp[$j]
    Next
Next
 _ArraySort($aLog, 1, 0, 0, 2)
 _ArrayDisplay($aLog, "Montly Expenses  budget log ")

 Local $aKeys[1] = [0]

For $i =  0 To UBound($aLog) - 1
If $aLog[$i][3] = "" Then
Assign("INDEX_" & $aLog[$i][$iValues2_Type], $i )
    If IsDeclared("ITEM_" & $aLog[$i][$iValues2_Type]) Then
        Assign("ITEM_" & $aLog[$i][$iValues2_Type], Eval("ITEM_" & $aLog[$i][$iValues2_Type]) + $aLog[$i][$iValues2_Price] )
    Else
        Redim $aKeys[ UBound($aKeys) + 1]
        $aKeys[UBound($aKeys) - 1] = $aLog[$i][$iValues2_Type]
        $aKeys[0] += 1
        Assign("ITEM_" & $aLog[$i][$iValues2_Type],$aLog[$i][$iValues2_Price] )
     EndIf

EndIf
Next

For $i = 1 To $aKeys[0]
            $aLog[ Eval("INDEX_" & $aKeys[$i])][$iValues2_SumLOG] = Eval("ITEM_" & $aKeys[$i])
                Next
  _ArrayDisplay($aLog, "Montly Expenses  budget log summaried BEFORE sorted col3")

  Local $iCount = 0, $sTemp

For $i = 0 To UBound($aLog, 1) - 1
    $iDelete = 0

    For $j = 0 To UBound($aLog, 2) - 1
        $aLog[ $iCount][$j] =$aLog[$i][$j]
        If $aLog[$i][$j] == 0 OR $aLog[$i][$j] = "" Then $iDelete = 1
    Next
    If NOT $iDelete  Then  $iCount += 1
Next

Redim $aLog[ $iCount][UBound($aLog, 2) ]

    _ArrayDisplay($aLog, "Montly Expenses  budget log summarized blanks deleted")

 _ArraySort($aLog, 1, 0, 0, 3)
  _ArrayDisplay($aLog, "Montly Expenses  budget log summarized sorted col3")
  ;above displayed array is NOT sorted in descending order on col 3 ??????????

;******************************
;******************************
;******************************
;setup next array for next steps, can't go further with the above problems
 dim $alogs [UBound($alog)] [4]
for $i =0 to UBound($alog)-1
    $alogs[$i][0] = $alog [$i] [2]
    $alogs [$i] [2] = $alog[$i] [3]

    Next

_ArraySort($aLogs, 1, 0, 0, 2)
_ArrayDisplay($aLogs, "My Budget sorted final ")
;above displayed array is NOT sorted in descending order on col 3 , see previous array.

 

Link to comment
Share on other sites

  • Solution

You need to convert the string values to numbers.

;<<<<<<<<<<<<<<<<<<<<<<<<
#include <Array.au3>
#include <File.au3>
#include <Misc.au3>
#include <FileConstants.au3>


Global Enum $iValues_Col0, $iValues_Col1, $iValues_Col2, $iValues_Col3, $iValues_Price, $iValues_Type, $iValues_Sum,$iValues_SumLOG
Global Enum $iValues2_ven, $iValues2_price, $iValues2_type,$iValues2_SumLOG



Local $budgetlogfile = "test.csv" ;this is where I have the data stored, example below.

$csv = FileRead ($budgetlogfile)
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $aLog[$rows[0] + 1][4]                    ;8 for columns adjust
$aLog[2][0] = $rows[0]  ;$aLog[0][0] = $rows[0]
For $i = 1 to $rows[0] ;For $i = 1 to $rows[0]

    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1

        $aLog[$i][$j] = $temp[$j]
    Next
Next
 _ArraySort($aLog, 1, 0, 0, 2)
 _ArrayDisplay($aLog, "Montly Expenses  budget log ")

 Local $aKeys[1] = [0]

For $i =  0 To UBound($aLog) - 1
If $aLog[$i][3] = "" Then
Assign("INDEX_" & $aLog[$i][$iValues2_Type], $i )
    If IsDeclared("ITEM_" & $aLog[$i][$iValues2_Type]) Then
        Assign("ITEM_" & $aLog[$i][$iValues2_Type], Eval("ITEM_" & $aLog[$i][$iValues2_Type]) + $aLog[$i][$iValues2_Price] )
    Else
        Redim $aKeys[ UBound($aKeys) + 1]
        $aKeys[UBound($aKeys) - 1] = $aLog[$i][$iValues2_Type]
        $aKeys[0] += 1
        Assign("ITEM_" & $aLog[$i][$iValues2_Type],$aLog[$i][$iValues2_Price] )
     EndIf

EndIf
Next

For $i = 1 To $aKeys[0]
            $aLog[ Eval("INDEX_" & $aKeys[$i])][$iValues2_SumLOG] = Eval("ITEM_" & $aKeys[$i])
                Next
  _ArrayDisplay($aLog, "Montly Expenses  budget log summaried BEFORE sorted col3")

  Local $iCount = 0, $sTemp

For $i = 0 To UBound($aLog, 1) - 1
    $iDelete = 0

    For $j = 0 To UBound($aLog, 2) - 1
        $aLog[ $iCount][$j] =$aLog[$i][$j]
        If $aLog[$i][$j] == 0 OR $aLog[$i][$j] = "" Then $iDelete = 1
    Next
    If NOT $iDelete  Then  $iCount += 1
Next

Redim $aLog[ $iCount][UBound($aLog, 2) ]

    _ArrayDisplay($aLog, "Montly Expenses  budget log summarized blanks deleted")

;========================= FIX FROM CZARDAS ============================
For $i = 0 To UBound($aLog) -1
    $aLog[$i][3] = Number($aLog[$i][3])
Next
;=======================================================================

 _ArraySort($aLog, 1, 0, 0, 3)
  _ArrayDisplay($aLog, "Montly Expenses  budget log summarized sorted col3")
  ;above displayed array is NOT sorted in descending order on col 3 ??????????

;******************************
;******************************
;******************************
;setup next array for next steps, can't go further with the above problems
 dim $alogs [UBound($alog)] [4]
for $i =0 to UBound($alog)-1
    $alogs[$i][0] = $alog [$i] [2]
    $alogs [$i] [2] = $alog[$i] [3]

    Next

_ArraySort($aLogs, 1, 0, 0, 2)
_ArrayDisplay($aLogs, "My Budget sorted final ")
;above displayed array is NOT sorted in descending order on col 3 , see previous array.
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...