Skip to content

Commit

Permalink
Replace sort routines with stable sort ones
Browse files Browse the repository at this point in the history
  • Loading branch information
xhero committed Feb 24, 2016
1 parent 4ecba90 commit a4e8521
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/marc_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function serialize_element( element, tag, json_marc, toplevel_groups) {
// to its own function

subfields = order_subfields(subfields_unordered);

// Build the JSON marc tag
marc_tag = {};
// subfields are an array of objects
Expand Down
13 changes: 10 additions & 3 deletions lib/marc_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,13 @@ def children

# Sort children and return them. can be used as block
def for_every_child_sorted
n = 0
if block_given?
@children.sort { |a, b| (a.tag.match(/\d/) ? "z#{a.tag}" : a.tag) <=> (b.tag.match(/\d/) ? "z#{b.tag}" : b.tag) }.each { |child| yield child }
@children.sort_by {|a| n += 1; [(a.tag.match(/\d/) ? "z#{a.tag}" : a.tag), n]}.each { |child| yield child }
#@children.sort { |a, b| (a.tag.match(/\d/) ? "z#{a.tag}" : a.tag) <=> (b.tag.match(/\d/) ? "z#{b.tag}" : b.tag) }.each { |child| yield child }
else
@children.sort { |a, b| (a.tag.match(/\d/) ? "z#{a.tag}" : a.tag) <=> (b.tag.match(/\d/) ? "z#{b.tag}" : b.tag) }
#@children.sort { |a, b| (a.tag.match(/\d/) ? "z#{a.tag}" : a.tag) <=> (b.tag.match(/\d/) ? "z#{b.tag}" : b.tag) }
@children.sort_by {|a| n += 1; [(a.tag.match(/\d/) ? "z#{a.tag}" : a.tag), n]}
end
end

Expand Down Expand Up @@ -519,7 +522,11 @@ def add_at(child, index)
end

def sort_alphabetically
@children = @children.sort { |a, b| (a.tag.match(/\d/) ? "z#{a.tag}" : a.tag) <=> (b.tag.match(/\d/) ? "z#{b.tag}" : b.tag) }
n = 0
@children = @children.sort_by {|a|
n += 1
[(a.tag.match(/\d/) ? "z#{a.tag}" : a.tag), n]
}
end

alias length size
Expand Down

0 comments on commit a4e8521

Please sign in to comment.