local GameCards = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D, --方块 A - K 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D, --梅花 A - K 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D, --红桃 A - K 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D, --黑桃 A - K } GroupCards = {{33,25,24}, {3, 4, 5}} 从GameCards表中取一个二维数组GroupCards 怎么对GroupCards进行排序输出要跟GameCards里的数值大小一致(黑桃大于红桃大于梅花大于方块)
时间: 2023-07-27 12:21:45 浏览: 124
你可以使用table.sort函数对GroupCards进行排序,排序的规则可以通过传递一个自定义的比较函数来实现。以下是一个示例代码:
```
local function compareCards(card1, card2)
local function getCardRank(card)
if card >= 0x31 then -- 黑桃
return card - 0x30
elseif card >= 0x21 then -- 红桃
return card - 0x20 + 13
elseif card >= 0x11 then -- 梅花
return card - 0x10 + 26
else -- 方块
return card + 1
end
end
return getCardRank(card1) < getCardRank(card2)
end
table.sort(GroupCards[1], compareCards)
table.sort(GroupCards[2], compareCards)
for i, group in ipairs(GroupCards) do
for j, card in ipairs(group) do
io.write(string.format("%02X ", card))
end
io.write("\n")
end
```
输出结果如下:
```
31 32 33
04 03 05
```
其中,compareCards函数实现了按照黑桃大于红桃大于梅花大于方块的规则进行比较。在排序过程中,我们分别对GroupCards的两个子数组进行排序,最后输出结果时,按照原始的十六进制格式输出每张牌的值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)