如何对逗号分隔的字符串进行排序?

时间:2014-06-30 19:10:35

标签: ruby sorting

我有以下Ruby代码:

settings= hash.new
settings= batch.getPartialSettings
settings= batchSettings.merge(batch.getEntireSettings)
puts settings

结果是:

{"Resolution"=>"1024", "Applications"=>"Mozilla,IE,Chrome", "Programming"=>"Java,HTML"}

我希望将"Applications"排序为:

"Applications"=>"Chrome,IE,Mozilla" 

所以,我的最终结果应该是:

{"Resolution"=>"1024", "Applications"=>"Chrome,IE,Mozilla", "Programming"=>"Java,HTML"}

1 个答案:

答案 0 :(得分:3)

unsorted_apps = settings['Applications']
sorted_apps = unsorted_apps.split(',').sort.join(',')
settings['Applications'] = sorted_apps