From d58c83fbd8947323a4c2d5df579a41b82497686e Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Fri, 27 Mar 2020 04:45:03 +0000 Subject: [PATCH] Use BigInt when sorting filenames (#22) --- spec/util_spec.cr | 8 ++++++++ src/util.cr | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/util_spec.cr b/spec/util_spec.cr index b2f2c2c..47e26d6 100644 --- a/spec/util_spec.cr +++ b/spec/util_spec.cr @@ -25,4 +25,12 @@ describe "compare_alphanumerically" do compare_alphanumerically a, b }.should eq ary end + + # https://github.com/hkalexling/Mango/issues/22 + it "handles numbers larger than Int32" do + ary = ["14410155591588.jpg", "21410155591588.png", "104410155591588.jpg"] + ary.reverse.sort {|a, b| + compare_alphanumerically a, b + }.should eq ary + end end diff --git a/src/util.cr b/src/util.cr index 2370c40..ca7b08d 100644 --- a/src/util.cr +++ b/src/util.cr @@ -1,3 +1,5 @@ +require "big" + IMGS_PER_PAGE = 5 macro layout(name) @@ -56,7 +58,7 @@ def compare_alphanumerically(c, d) return -1 if a.nil? return 1 if b.nil? if is_numeric(a) && is_numeric(b) - compare = a.to_i <=> b.to_i + compare = a.to_big_i <=> b.to_big_i return compare if compare != 0 else compare = a <=> b