From 70b19c238a6be78d39881a419546edddc61a1c41 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 10 Apr 2016 11:18:40 -0400 Subject: [PATCH] Sort types/categories by number when they start with a number --- lib/docs/core/models/type.rb | 8 +++++++- test/lib/docs/core/models/type_test.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/docs/core/models/type.rb b/lib/docs/core/models/type.rb index 2460250b..b9869ae7 100644 --- a/lib/docs/core/models/type.rb +++ b/lib/docs/core/models/type.rb @@ -7,8 +7,14 @@ module Docs self.count ||= 0 end + STARTS_WITH_INTEGER = /\A\d/ + def <=>(other) - name.to_s.casecmp(other.name.to_s) + if name && other && name =~ STARTS_WITH_INTEGER && other.name =~ STARTS_WITH_INTEGER + name.to_i <=> other.name.to_i + else + name.to_s.casecmp(other.name.to_s) + end end def slug diff --git a/test/lib/docs/core/models/type_test.rb b/test/lib/docs/core/models/type_test.rb index 31951685..8450def7 100644 --- a/test/lib/docs/core/models/type_test.rb +++ b/test/lib/docs/core/models/type_test.rb @@ -21,14 +21,17 @@ class DocsTypeTest < MiniTest::Spec describe "#<=>" do it "returns 1 when the other type's name is less" do assert_equal 1, Type.new('b') <=> Type.new('a') + assert_equal 1, Type.new('15 a') <=> Type.new('4 b') end it "returns -1 when the other type's name is greater" do assert_equal -1, Type.new('a') <=> Type.new('b') + assert_equal -1, Type.new('8 a') <=> Type.new('16 b') end it "returns 0 when the other type's name is equal" do assert_equal 0, Type.new('a') <=> Type.new('a') + assert_equal 0, Type.new('23 a') <=> Type.new('23 b') end it "is case-insensitive" do