Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/extensions.rb

Constant Summary collapse

KW_ORDER =

Sort values, but impose special keyword ordering

Yields:

  • a, b

Yield Parameters:

  • a (Object)
  • b (Object)

Yield Returns:

  • (Integer)

Returns:

%w(@base @id @value @type @language @vocab @container @graph @list @set @index).freeze

Instance Method Summary collapse

Instance Method Details

#kw_sortObject

Order, considering keywords to come before other strings



76
77
78
79
80
81
82
# File 'lib/json/ld/extensions.rb', line 76

def kw_sort
  self.sort do |a, b|
    a = "@#{KW_ORDER.index(a)}" if KW_ORDER.include?(a)
    b = "@#{KW_ORDER.index(b)}" if KW_ORDER.include?(b)
    a <=> b
  end
end

#term_sortObject

Order terms, length first, then lexographically



85
86
87
88
89
90
# File 'lib/json/ld/extensions.rb', line 85

def term_sort
  self.sort do |a, b|
    len_diff = a.length <=> b.length
    len_diff == 0 ? a <=> b : len_diff
  end
end