在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ruby中的sort方法,这个方法可以加一个两个参数的block,这个block可以返回1 0 -1来表示这两个参数大于 等于 小于 str = ["192.160.175","192.160.90","192.168.2.175","192.168.2.90","192.168.1.180","192.160.90.1"] arr = str.sort do |a, b| ip1 = a.split('.').map &:to_i ip2 = b.split('.').map &:to_i ret = 0 ip1.each_with_index do |ip, i| if ip2[i].nil? ret = 1 break end if ip != ip2[i] ret = ip <=> ip2[i] break end end ret = ret == 0 && ip1.size > ip2.size ? 1 : ret end p arr 输出: ["192.160.90","192.160.175","192.160.90.1","192.168.1.180","192.168.2.90","192.168.2.175"]
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论