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



53
54
55
56
57
58
59
# File 'lib/json/ld/extensions.rb', line 53

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



62
63
64
65
66
67
# File 'lib/json/ld/extensions.rb', line 62

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