<commit_before># frozen_string_literal: true

require_relative '../../helpers/transform_to_sortable_numeric'

class ActiveSet
  module Sorting
    class EnumerableStrategy
      def initialize(set, attribute_instructions)
        @set = set
        @attribute_instructions = attribute_instructions
      end

      def execute
        # http://brandon.dimcheff.com/2009/11/18/rubys-sort-vs-sort-by/
        @set.sort_by do |item|
          @attribute_instructions.map do |instruction|
            value_for_comparison = sortable_numeric_for(instruction, item)
            direction_multiplier = direction_multiplier(instruction.value)

            # in an ASC sort, nils float to the end of the list. In a DESC
            # sort, nils float to the start of the list. This is achieved by
            # wrapping each value_for_comparison in a tuple with 0 as the first
            # element, and wrapping nil values with either 1 or -1 as the first
            # element
            if value_for_comparison.nil?
              [direction_multiplier, 0]
            else
              [0, value_for_comparison * direction_multiplier]
            end
          end
        end
      end

      def sortable_numeric_for(instruction, item)
        value = instruction.value_for(item: item)
        if value.is_a?(String) || value.is_a?(Symbol)
          value = if case_insensitive?(instruction, value)
                    value.to_s.downcase
                  else<commit_msg>Update the comment on the Enumerable sorting operation to match the comment in the ActiveRecord operation<commit_after># frozen_string_literal: true

require_relative '../../helpers/transform_to_sortable_numeric'

class ActiveSet
  module Sorting
    class EnumerableStrategy
      def initialize(set, attribute_instructions)
        @set = set
        @attribute_instructions = attribute_instructions
      end

      def execute
        # http://brandon.dimcheff.com/2009/11/18/rubys-sort-vs-sort-by/
        @set.sort_by do |item|
          @attribute_instructions.map do |instruction|
            value_for_comparison = sortable_numeric_for(instruction, item)
            direction_multiplier = direction_multiplier(instruction.value)

            # Force null values to be sorted as if larger than any non-null value
            # ASC => [-2, -1, 1, 2, nil]
            # DESC => [nil, 2, 1, -1, -2]
            if value_for_comparison.nil?
              [direction_multiplier, 0]
            else
              [0, value_for_comparison * direction_multiplier]
            end
          end
        end
      end

      def sortable_numeric_for(instruction, item)
        value = instruction.value_for(item: item)
        if value.is_a?(String) || value.is_a?(Symbol)
          value = if case_insensitive?(instruction, value)
                    value.to_s.downcase
                  else
{'fraction_non_alphanumeric': 0.07225130890052356, 'fraction_numerical': 0.010820244328097731, 'mean_word_length': 2.156387665198238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4254895', 'n_tokens_mistral': 814, 'n_tokens_neox': 778, 'n_words': 212}