From 977d3beb2dcb8ccbba6b7bd140aacaa5475de680 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Fri, 28 May 2021 13:20:00 +0900 Subject: [PATCH] count: if the values are the same, sort by name --- lib/youplot/backends/processing.rb | 9 +++++---- test/fixtures/simple-count.txt | 18 +++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/youplot/backends/processing.rb b/lib/youplot/backends/processing.rb index 317ed37..c6b8ca2 100644 --- a/lib/youplot/backends/processing.rb +++ b/lib/youplot/backends/processing.rb @@ -14,11 +14,12 @@ module YouPlot # value_counts Enumerable::Statistics arr.value_counts(dropna: false) end - .sort { |a, b| a[1] <=> b[1] } - .reverse + .sort do |a, b| + r = b[1] <=> a[1] # compare values + r = a[0] <=> b[0] if r == 0 # If the values are the same, compare by name + r + end .transpose - # faster than `.sort_by{|a| a[1]}`, `.sort_by(a:last)` - # `.sort{ |a, b| -a[1] <=> -b[1] } end end end diff --git a/test/fixtures/simple-count.txt b/test/fixtures/simple-count.txt index 4509db6..2ac8f4c 100644 --- a/test/fixtures/simple-count.txt +++ b/test/fixtures/simple-count.txt @@ -1,12 +1,12 @@ ┌ ┐ - 50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - -50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - 40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - -40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - 30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - -30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - 20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - -20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 - 10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 -10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + -20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + -30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + -40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + -50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + 10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + 20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + 30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + 40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 + 50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 └ ┘