Since the fields property of a MARC::Record is a MARC::FieldMap, which is a
subclass of Array, I use the Array.sort_by! method:
record.fields.sort_by! {|f| f.tag}
On Fri, Sep 12, 2014 at 12:28 AM, Jason Stirnaman <[log in to unmask]>
wrote:
> Ruby-marc sages,
> What's the best way to re-sequence fields in a record after appending to
> it? This seems to work ok, but feels wrong.
>
>
> for record in reader
> # Return a record with new field appended.
> newrecord = add_control_number(record)
>
> ### Re-sort fields by tag and copy them to a new record. ###
> sortedrecord = MARC::Record.new
> sortedrecord.leader = newrecord.leader
> newrecord.sort_by{|f| f.tag}.each {|tag| sortedrecord.append(tag)}
>
> writer.write(sortedrecord)
> end
>
>
> Thanks,
> Jason
>
> Jason Stirnaman
> Lead, Library Technology Services
> University of Kansas Medical Center
> [log in to unmask]
> 913-588-7319
>
|