我正在寻找一种使用一个数组对另一个数组进行排序的最佳方法。下面的代码可以工作,但是性能较差,因为它是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
以上结果将返回正确排序的新数组:
re_ordered_array.flatten.compact
["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]
为改善按键的重新排序/排序,我尝试了以下方法
hash = use_for_ordering.each_with_index.to_h
上面的哈希打印出来
{"title"=>0, "creator"=>1, "alternate_identifier"=>2, "keyword"=>3}
然后我在下面重新排序时在这里使用
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
结果就是这个
re_ordered_array.compact
给出
["title", "creator_2", "creator_1", "keyword_2", "keyword_1", "alternate_identifier"]
新数组的问题是creator_1应该在creator_2之前,相同的情况适用于keyword_1和keyword_2
但是我想要的结果在下面,但是没有O(n ^ 2)或多次求助于数组
["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]
更新后的数组具有更好的代表性数据
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"]
答案 0 :(得分:1)
您可能想预先准备正则表达式数组,而不是首先在每次迭代时都不用字符串创建它们。
use_for_ordering =
%w[title creator alternate_identifier keyword].
map(&Regexp.method(:new))
#⇒ [/title/, /creator/, /alternate_identifier/, /keyword/]
或更妙的是,创建枚举器:
use_for_ordering =
%w[title creator alternate_identifier keyword].
map(&Regexp.method(:new)).each_with_index
#⇒ #<Enumerator: ...>
现在您可以将值分组,然后按索引排序:
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"]
答案 1 :(得分:1)
我可以建议以下选项:
设置
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}
如果需要,您可以在下一步之前对array_to_reorder
的值进行排序。
排序
array_to_reorder.sort_by { |k, _| use_for_ordering[k] }.flat_map(&:last)
#=> ["title", "creator_1", "creator_2", "alternate_identifier", "keyword_1", "keyword_2"]