簡體   English   中英

紅寶石排序以使用另一個數組重新排序數組元素

[英]ruby sorting to re-order array elements using another array

我正在尋找一種使用一個數組對另一個數組進行排序的最佳方法。 下面的代碼可以工作,但是性能較差,因為它是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"]

您可能需要預先准備正則表達式數組,而不是首先在每次迭代時都不用字符串創建它們。

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"]

我可以建議這個選項:

設定

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的值進行排序。

排序

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM