简体   繁体   中英

ruby sorting to re-order array elements using another array

I am looking for an optimal way to use one array to sort another. The code below works but poor performance wise because it is O(n^2).

re_ordered_array = []

use_for_ordering = ["title", "creator", 'alternate_identifier', "keyword"]

array_to_reorder = ["keyword_1", "title", "creator_1", "keyword_2", "creator_2", 'alternate_identifier']

use_for_ordering.each do |value|  
  re_ordered_array << array_to_reorder.select {|r| r =~ /#{value}/} 
end

The result of the above will return the new array ordered correctly:

re_ordered_array.flatten.compact

["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]

To improve how re-order/resort the keys I tried the approach below

hash = use_for_ordering.each_with_index.to_h

The hash above prints out

    {"title"=>0, "creator"=>1, "alternate_identifier"=>2, "keyword"=>3}

which i then use here when re-sorting below

array_to_reorder.sort.each do |k| 
   use_for_ordering.any? do |i| 
     re_ordered_array.insert(hash[i], k) if  k =~ /#{i}/  
  end
end

The outcome is this

 re_ordered_array.compact

which gives

 ["title", "creator_2", "creator_1", "keyword_2", "keyword_1", "alternate_identifier"]

The problem with the new array is that creator_1 should be before creator_2, same thing applies to keyword_1 and keyword_2

but the end result of what with I want is below but without O(n^2) or resort the array multiple time

 ["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]

updated array with better representative data

 representative_array_order =  ["id", "date_uploaded", "date_modified", "file_url", "visibility", "embargo_end_date", "visibility_after_embargo", "lease_end_date", "visibility_after_lease", "collection", "work_type", "resource_type", "title", "creator", "contributor", "doi", "alternate_identifier", "version", "related_identifier", "series_name", "volume", "edition", "journal_title", "book_title", "issue", "pagination", "editor", "publisher", "place_of_publication", "isbn", "issn", "eissn", "article_number", "material_media", "date_published", "date_accepted", "date_submitted", "abstract", "keyword", "institution", "organisational_unit", "peer_reviewed", "official_url", "related_url", "related_exhibition", "related_exhibition_date", "project_name", "funder", "funder_project_reference", "additional_information", "license", "rights_statement", "rights_holder", "language", "event_title", "event_date", "event_location"]

 representative_array_for_recorder = ["file_1", "id", "title_1", "date_uploaded", "date_modified", "account_cname", "institution_1", "institution_2", "institution_3", "institution_4", "institution_5", "funder_1", "funder_2", "date_published", "date_accepted", "date_submitted", "project_name_1", "project_name_2", "rights_holder_1", "rights_holder_2", "doi", "place_of_publication_1", "place_of_publication_2", "abstract", "alternate_identifier_1", "alternate_identifier_type_1", "alternate_identifier_2", "alternate_identifier_type_2", "alternate_identifier_3", "alternate_identifier_type_3", "related_identifier_type_1", "related_identifier_id_1", "related_identifier_relationship_1", "related_identifier_type_2", "related_identifier_id_2", "related_identifier_relationship_2", "library_of_congress_classification_1", "library_of_congress_classification_2", "alt_title_1", "alt_title_2", "volume_1", "pagination", "issn", "eissn", "edition", "event_title", "event_date", "event_location", "book_title", "journal_title", "issue", "isbn", "related_exhibition", "related_exhibition_date", "version", "alternative_journal_title_1", "alternative_journal_title_2", "resource_type_1", "creator_family_name_1", "creator_name_type_1", "creator_orcid_1", "creator_isni_1", "creator_position_1", "creator_given_name_2", "creator_family_name_2", "creator_name_type_2", "creator_orcid_2", "creator_isni_2", "creator_position_2", "creator_name_type_3", "creator_isni_3", "creator_position_3", "creator_organisation_name_3", "contributor_given_name_1", "contributor_family_name_1", "contributor_name_type_1", "contributor_orcid_1", "contributor_isni_1", "contributor_position_1", "contributor_name_type_2", "contributor_isni_2", "contributor_position_2", "contributor_organisation_name_2", "description", "keyword_1", "keyword_2", "keyword_3", "license_1", "license_2", "rights_statement_1", "publisher_1", "date_created", "subject", "language", "related_url_1", "related_url_2", "source", "embargo_end_date", "lease_end_date", "visibility", "visibility_after_embargo", "work_type", "visibility_after_lease", "collection", "organisation_unit_1", "organisation_unit_2", "peer_reviewed", "funder_project_reference_1", "funder_project_reference_2", "additional_information", "official_url", "article_number", "material_media"]

You probably want to prepare the array of regexps upfront instead of creating them out of strings on each iteration in the first place.

use_for_ordering = 
  %w[title creator alternate_identifier keyword].
    map(&Regexp.method(:new))
#⇒ [/title/, /creator/, /alternate_identifier/, /keyword/]

Or, even better, create the enumerator:

use_for_ordering = 
  %w[title creator alternate_identifier keyword].
    map(&Regexp.method(:new)).each_with_index
#⇒ #<Enumerator: ...>

Now you might group values and then sort by index:

array_to_reorder.
  group_by do |e|
    key = use_for_ordering.find { |r, i| r =~ e  }
    key.nil? ? Float::INFINITY : key.last
  end.sort.flat_map(&:last)
#⇒ ["title", "creator_1", "creator_2",
#   "alternate_identifier", "keyword_1", "keyword_2"]

I can suggest this option:

Setup

array_to_reorder = use_for_ordering.each_with_object({}) { |k, h| h[k] =  array_to_reorder.select { |e| e.start_with? k } }
#=> {"title"=>["title"], "creator"=>["creator_1", "creator_2"], "alternate_identifier"=>["alternate_identifier"], "keyword"=>["keyword_1", "keyword_2"]}
use_for_ordering = use_for_ordering.map.zip(0..).to_h
#=> {"title"=>0, "creator"=>1, "alternate_identifier"=>2, "keyword"=>3}

You could sort the values of array_to_reorder before the next step, if required.

Sorting

array_to_reorder.sort_by { |k, _| use_for_ordering[k] }.flat_map(&:last)
#=> ["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM