Add reverse to count

This commit is contained in:
kojix2
2021-05-31 22:50:11 +09:00
parent 49626d54e1
commit 4651c697d4
7 changed files with 89 additions and 28 deletions

View File

@@ -6,22 +6,30 @@ module YouPlot
module Processing
module_function
def count_values(arr, tally: true)
def count_values(arr, tally: true, reverse: false)
# tally was added in Ruby 2.7
if tally && Enumerable.method_defined?(:tally)
arr.tally
else
# value_counts Enumerable::Statistics
arr.value_counts(dropna: false)
end
.sort do |a, b|
# compare values
r = b[1] <=> a[1]
# If the values are the same, compare by name
r = a[0] <=> b[0] if r == 0
r
result = \
if tally && Enumerable.method_defined?(:tally)
arr.tally
else
# value_counts Enumerable::Statistics
arr.value_counts(dropna: false)
end
.transpose
# sorting
result = result.sort do |a, b|
# compare values
r = b[1] <=> a[1]
# If the values are the same, compare by name
r = a[0] <=> b[0] if r == 0
r
end
# --reverse option
result.reverse! if reverse
# prepare for barplot
result.transpose
end
end
end