Thanks, @david! That was a huge help.
This will find all categories that match search and sort them and the subcategories that they contain.
def sort_matching_subcategories(search)
categories = Category.where("name like ?", search)
position = 100
categories.order(:name).each do |cat|
position += 1
cat.position = position
cat.save!()
c_position = 0
children = Category.where(:parent_category_id=>cat.id)
children.order(:name).each do |c|
c_position += 1
c.position = c_position
c.save!()
end
end
end