1

概括:

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

背景和目标:

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

我已经实现的是:

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

但是我想要实现的内容显示在底部的示例表中。请注意,该示例仅包含示例数据。作为测试用例结果,真正的数组内容将在数组中包含更多的列和行。但这应该足以理解这个想法。

代码:

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

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 |

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

4

2 回答 2

2

这符合您的期望吗?

#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' )

久违了,美嘉

于 2019-02-07T13:43:48.327 回答
0

是的:

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' )
于 2019-02-07T14:06:26.307 回答