红宝石中的版本排序(带有Alpha,Beta等) [英] Version sort (with alphas, betas, etc.) in ruby
本文介绍了红宝石中的版本排序(带有Alpha,Beta等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Ruby中对版本列表进行排序?我已经看过有关自然排序的内容,但这已经超出了这一步.
How do I sort a list of versions in Ruby? I've seen stuff about natural sort, but this is a step beyond that.
输入是一串这样的字符串:
Input is a bunch of strings like this:
input = ['10.0.0b12', '10.0.0b3', '10.0.0a2', '9.0.10', '9.0.3']
我几乎可以使用自然 gem做到这一点:
I can almost do it with the naturally gem:
require 'naturally'
Naturally.sort(input)
=> ["9.0.3", "9.0.10", "10.0.0a2", "10.0.0b12", "10.0.0b3"]
问题:10.0.0b3在10.0.0b12之后排序; 10.0.0b3应该是第一个.
Problem: 10.0.0b3 is sorted after 10.0.0b12; 10.0.0b3 should be first.
任何人都有可行的方法吗?其他语言也有帮助!
Anyone have a way that works? Other languages are helpful too!
推荐答案
Ruby随Gem类一起提供,该类了解版本:
Ruby ships with the Gem class, which knows about versions:
ar = ['10.0.0b12', '10.0.0b3', '10.0.0a2', '9.0.10', '9.0.3']
p ar.sort_by { |v| Gem::Version.new(v) }
# => ["9.0.3", "9.0.10", "10.0.0a2", "10.0.0b3", "10.0.0b12"]
这篇关于红宝石中的版本排序(带有Alpha,Beta等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文