Class: Ace::Support::Items::Molecules::SmartSorter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/items/molecules/smart_sorter.rb

Overview

Sorts items using pinned-first + auto-sort logic. Pinned items (those with a position value) sort first by position ascending. Unpinned items sort second by computed score descending.

Class Method Summary collapse

Class Method Details

.sort(items, score_fn:, pin_accessor:) ⇒ Array

Returns Pinned items (by position asc) + unpinned items (by score desc).

Parameters:

  • items (Array)

    Items to sort

  • score_fn (Proc)

    ->(item) { Float } computes auto-sort score

  • pin_accessor (Proc)

    ->(item) { String|nil } reads position field

Returns:

  • (Array)

    Pinned items (by position asc) + unpinned items (by score desc)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ace/support/items/molecules/smart_sorter.rb', line 15

def self.sort(items, score_fn:, pin_accessor:)
  return [] if items.nil? || items.empty?

  pinned = []
  unpinned = []

  items.each do |item|
    pos = pin_accessor.call(item)
    if pos && !pos.to_s.empty?
      pinned << item
    else
      unpinned << item
    end
  end

  sorted_pinned = pinned.sort_by { |item| pin_accessor.call(item).to_s }
  sorted_unpinned = unpinned.sort_by { |item| -score_fn.call(item) }

  sorted_pinned + sorted_unpinned
end