gpt4 book ai didi

Ruby:对不包括某些符号的数组进行排序

转载 作者:数据小太阳 更新时间:2023-10-29 07:45:08 26 4
gpt4 key购买 nike

我有这样一个数组:["c", "-b", "a"]

["c", "-b", "a"].sort! 返回 ["-b", "a", "c"]

我希望它像 ["a", "-b", "c"] 一样排序

如何从排序中排除负号?获取它的最简单方法是什么?

最佳答案

["c", "-b", "a"].sort_by{|e| e[/\w+/] }
# => ["a", "-b", "c"]

关于Ruby:对不包括某些符号的数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com