Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort "chapter numbers" like 1.1.1, 1.2.1, 1.2.46, etc.?

Tags:

sorting

ruby

Is there a quick way to sort "chapter numbers" in Ruby?

1.1.1
1.1.2
1.2.1
1.3.1
1.3.2
1.3.3
10.42.64
etc.?

Do I have to write an enumerator or something like that?

like image 398
Joshua Muheim Avatar asked Jan 21 '26 15:01

Joshua Muheim


1 Answers

In Ruby, Arrays are ordered lexicographically, so the easiest way would be to convert these into Arrays and then sort them:

chapters = %w[1.1.1 1.1.2 1.2.1 1.3.1 1.3.2 1.3.3 10.42.64]

chapters.sort_by {|chapter| chapter.split('.').map(&:to_i) }
# => ["1.1.1", "1.1.2", "1.2.1", "1.3.1", "1.3.2", "1.3.3", "10.42.64"]

Of course, the real solution would be to use objects instead of slugging around arrays of strings of numbers. After all, Ruby is an object-oriented language, not an arrays-of-strings-of-numbers-oriented language:

class ChapterNumber
  include Comparable

  def initialize(*nums)
    self.nums = nums
  end

  def <=>(other)
    nums <=> other.nums
  end

  def to_s
    nums.join('.')
  end

  alias_method :inspect, :to_s

  protected

  attr_reader :nums

  private

  attr_writer :nums
end

chapters = [ChapterNumber.new(1, 1, 1), ChapterNumber.new(1, 1, 2), 
  ChapterNumber.new(1, 2, 1), ChapterNumber.new(1, 3, 1), 
  ChapterNumber.new(1, 3, 2), ChapterNumber.new(1, 3, 3), 
  ChapterNumber.new(10, 42, 64)]

chapters.sort
# => [1.1.1, 1.1.2, 1.2.1, 1.3.1, 1.3.2, 1.3.3, 10.42.64]
like image 95
Jörg W Mittag Avatar answered Jan 23 '26 06:01

Jörg W Mittag