Jump to content

Sorting table stored in Array


fisofo
 Share

Recommended Posts

perhaps my answer lies with randallc's code for subsorting, but i'm just getting confused i'm afraid.

I want to sort a table i've grabbed off the web by it's first/second column, etc... but when i do this:

$aTableData = _IETableWriteToArray ($oTable)
_ArraySort($aTableData, 1, 0, -1, UBound($aTableData,2), 0)

it just rearranges the columns into different spots. I'm pretty tired, so hopefully this is making sense... i'll check in tomorrow with more info if what i provided is not enough. I believe my answer may lie with "$i_SortIndex," but i'm unsure... could someone describe it better than it's listed in help?

thanks!

Link to comment
Share on other sites

I may have found part of the problem, when i grab a table and put it in an array, it seems that it comes in transposed. Is that right? I wrote an example to show what I mean:

#include <IE.au3>
#include <Array.au3>

$oIE = _IECreate ("http://www.december.com/html/demo/tablehi.html", 0, 0)

$oTable = _IETableGetCollection ($oIE, 1)
$aTableData = _IETableWriteToArray ($oTable)
_IEQuit($oIE)

lf_Array2dDisplay($aTableData, "array", 0, 0)

_ArraySort($aTableData, 1, 0, -1, UBound($aTableData,2), 0)

lf_Array2dDisplay($aTableData, "array", 0, 0)

Func lf_Array2dDisplay(ByRef $ar_Array, $s_Title="Array contents", $n_Index=1,$i_Message=0)
    ; Display 2 dimensional array
    Local $output = ""
    Local $r, $c
    If Not IsArray($ar_Array) Then Return -1
    For $r = $n_Index to UBound($ar_Array,1) - 1
        $output = $output & @LF
        For $c = 0 to UBound($ar_Array,2) - 1
            $output = $output & $ar_Array[$r][$c] & " "
        Next
    Next
    ConsoleWrite( $s_Title&"="&@LF&$output&@LF)
    if $i_Message then MsgBox(4096, $s_Title, $output)
    Return $output
EndFunc   ;==>lf_Array2dDisplay

If you go to that website you can see an example table. Why is the sorting working the way it does, and why is the array transposed when it is displayed? (I borrowed the array2ddisplay function from randallc)

Edited by fisofo
Link to comment
Share on other sites

*bump* can anyone help out with this? :whistle:

I adjusted the code so that it prints the table properly from the array, but i still can't get the freaking sorting to work!

Func lf_Array2dDisplay(ByRef $ar_Array, $s_Title="Array contents", $n_Index=1,$i_Message=0)
    ; Display 2 dimensional array
    Local $output = ""
    Local $r, $c
    If Not IsArray($ar_Array) Then Return -1
    For $r = $n_Index to UBound($ar_Array,2) - 1
        $output = $output & @LF
        For $c = 0 to UBound($ar_Array,1) - 1
            $output = $output & $ar_Array[$c][$r] & " "
        Next
    Next
    ConsoleWrite( $s_Title&"="&@LF&$output&@LF)
    if $i_Message then MsgBox(4096, $s_Title, $output)
    Return $output
EndFunc   ;==>lf_Array2dDisplay

From the code I posted I get this output:

array=
1  3  2  
2  4  1  
3  0  8  
4  8  7  
5  2  1  

array=
2  3  1  
1  4  2  
8  0  3  
7  8  4  
1  2  5

The first is the original array, the second is the "sorted" array. What I'd like to get for the second array is this:

5 2 1

4 8 7

3 0 8

2 4 1

1 3 2

I'm gonna keep working on this, but i could really use some help ;)

Link to comment
Share on other sites

Okay, i finally got it. For those that need it, you need to use _Array2DTranspose($aTableData) before sorting... you can find it in randallc's forum i linked earlier... sheesh :whistle:

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