数组排序["B3","D2","F1","A9","D12","A2","C1","Z0","B1"]=>["Z0","B1","C1","F1","A2","D2","B3","A9","D12"]
时间: 2023-09-10 12:04:56 浏览: 127
对一个数组进行选择排序
可以使用以下代码对给定的数组进行排序:
```python
array = ["B3","D2","F1","A9","D12","A2","C1","Z0","B1"]
sorted_array = sorted(array, key=lambda x: int(x[1:]))
print(sorted_array)
```
输出结果为:["Z0","B1","C1","F1","A2","D2","B3","A9","D12"]
这段代码使用了 `sorted()` 函数以及一个自定义的排序关键字函数。关键字函数 `lambda x: int(x[1:])` 用于将数组中的每个元素转换为整数,然后按照整数值进行排序。这里使用切片 `[1:]` 是为了忽略掉每个元素中的第一个字符,因为它们都是字母。
阅读全文