On Wed Sep 02 13:35:34 2015, GSULLIVAN wrote:
Show quoted text> Strange. I looked like I uploaded both files. Here's the .pm patch
> attempt #2...
Ok thanks for the patch. I prefer to do a single sort, plus your patch doesn't yet handle the case of inf=number, so here's what I did:
------------------
Author: perlancar (@pc-office) <perlancar@gmail.com>
Date: Thu Sep 3 01:16:44 2015 +0700
Consistent ordering [problem reported by Gene Sullivan]
diff --git a/lib/Number/Closest/NonOO.pm b/lib/Number/Closest/NonOO.pm
index 73cfb8e..bdf04a8 100644
--- a/lib/Number/Closest/NonOO.pm
+++ b/lib/Number/Closest/NonOO.pm
@@ -62,9 +62,11 @@ sub _find {
$m;
} @nums;
#use Data::Dump; dd \@res;
- @res = sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]} @res;
+ @res = sort {$a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] || $a->[0] <=> $b->[0] }
+ @res;
} else {
- @res = sort {$a->[1] <=> $b->[1]} map {[$_, abs($_-$num)]} @nums;
+ @res = sort {$a->[1] <=> $b->[1] || $a->[0] <=> $b->[0]}
+ map {[$_, abs($_-$num)]} @nums;
}
@res = map {$_->[0]} @res;
diff --git a/t/01-basics.t b/t/01-basics.t
index 4c77430..c5930eb 100644
--- a/t/01-basics.t
+++ b/t/01-basics.t
@@ -19,4 +19,18 @@ is_deeply(find_closest_number(number=> "inf", numbers=>["-inf", -2, -1, 0, 1, 2,
is_deeply(find_closest_number(number=>"-inf", numbers=>["-inf", -2, -1, 0, 1, 2, "inf"], inf=>"number", items => 10),
["-inf", -2, -1, 0, 1, 2, "inf"], "inf=number 2");
+# ordering tests
+is(find_closest_number (number=>6, numbers=>[5, 7]), 5, "close order 1");
+is(find_closest_number (number=>6, numbers=>[7, 5]), 5, "close order 2");
+is(find_farthest_number(number=>6, numbers=>[5, 7]), 7, "far order 1");
+is(find_farthest_number(number=>6, numbers=>[7, 5]), 7, "far order 2");
+is_deeply(find_closest_number(number=> 6, numbers=>["-inf", 5, 7, "inf"], inf=>"number", items => 4),
+ [5, 7, "-inf", "inf"], "close order 1 (inf=number)");
+is_deeply(find_closest_number(number=> 6, numbers=>["-inf", 7, 5, "inf"], inf=>"number", items => 4),
+ [5, 7, "-inf", "inf"], "close order 2 (inf=number)");
+is_deeply(find_farthest_number(number=> 6, numbers=>["-inf", 5, 7, "inf"], inf=>"number", items => 4),
+ ["inf", "-inf", 7, 5], "far order 1 (inf=number)");
+is_deeply(find_farthest_number(number=> 6, numbers=>["-inf", 7, 5, "inf"], inf=>"number", items => 4),
+ ["inf", "-inf", 7, 5], "far order 2 (inf=number)");
+
done_testing;
------------------
Released 0.04, thanks!