AutoIt中如何对二维数组的列顺序进行排序

How to sort the column sequence of 2D array in AutoIt

总结:

我正在寻找一种在 AutoIt 中按列顺序对 2D(多维)数组进行排序的正确方法。我对 AutoIt 非常熟悉,并且有一个 _ArraySort 函数,它提供按列排序,从而导致排序的行。但我需要的是按列方向(按字母顺序)对列 order/sequence 进行排序。

背景和目标:

对于我创建的测试用例报告(HTML 文件),我需要在其中显示一个 table。这个table应该包含部分测试用例结果的数据。

我已经达到的是:

我将 CSV 文件读入数组(通过定义的分隔符),这不是问题所在。我可以为行序列对二维数组进行排序,并且为报告解析和转换为 HTML 结构也不是问题。

但是我想达到的效果显示在最下面的例子table中。请注意,该示例仅包含示例数据。作为测试用例结果,真正的数组内容将在数组中有更多的列和行。但这应该足以理解这个想法。

代码:

这只是示例数组数据,如下所示:

Global $a2dData[6][6]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

_ArrayDisplay( $a2dData )

那么,在AutoIt中如何对二维数组的列顺序进行排序呢?

排序列前的实际数组数据:

| fruits | IT manufactures | painters     | music genres | days     |
| apple  | Google          | Picasso      | Jazz         | Monday   |
| Orange | Microsoft       | Van Gogh     | Blues        | Thursday |
| Pear   |                 | Da Vinci     | Hip Hop      | Friday   |
|        | Apple           | Monet        | Rap          | Sunday   |
| Cherry | Adobe           | Michelangelo | Classic      |          |

列序列排序后的预期数组数据:

| days     | fruits | IT manufactures | music genres | painters     |
| Monday   | apple  | Google          | Jazz         | Picasso      |
| Thursday | Orange | Microsoft       | Blues        | Van Gogh     |
| Friday   | Pear   |                 | Hip Hop      | Da Vinci     |
| Sunday   |        | Apple           | Rap          | Monet        |
|          | Cherry | Adobe           | Classic      | Michelangelo |

是否已经有一个功能可以做到这一点,还是我必须自己做?

这是否符合您的预期?

#include <Array.au3>
Global $a2dData[6][5]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

_ArrayDisplay($a2dData, 'BEFORE')
_ArrayTranspose($a2dData)
_ArraySort($a2dData)
_ArrayTranspose($a2dData)
_ArrayDisplay($a2dData, 'AFTER' )

再见, 巨型

是:

Xenobiologist的解法完全符合我的预期,谢谢。但我不能用评论来表达。

其他解决方案:

同时我也自己解决了(但不是那种优雅的方式):

Global $a2dData[6][6]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

Func _arraySortColumnSequence( $a2dArray, $iAscOrDesc )
    Local $aJustColumns[1]
    For $i = 0 To UBound( $a2dArray, 2 ) - 2 Step 1
        _ArrayAdd( $aJustColumns, $a2dArray[0][$i] )
    Next

    _ArrayDelete( $aJustColumns, 0 )
    _ArraySort( $aJustColumns, $iAscOrDesc )

    Local $aNew2dArray[UBound( $a2dArray, 1 )][UBound( $a2dArray, 2 )]
    For $i = 0 To UBound( $aJustColumns ) - 1 Step 1
        For $j = 0 To UBound( $aJustColumns ) - 1 Step 1
            If $a2dArray[0][$j] = $aJustColumns[$i] Then
                For $k = 0 To UBound( $a2dArray, 1 ) - 1 Step 1
                    $aNew2dArray[$k][$i] = $a2dArray[$k][$j]
                Next
            EndIf
        Next
    Next

    Return $aNew2dArray
EndFunc


_ArrayDisplay( $a2dData, 'before' )

Global $a2dDataRes = _arraySortColumnSequence( $a2dData, 0 )

_ArrayDisplay( $a2dDataRes, 'after' )