0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

事前に定義されたリストの順番にそって、配列の要素を並べ替える簡単な方法

Last updated at Posted at 2022-05-19

例えばいくつかの都道府県を、事前に定義された順番に並べ替えたい時、

ISO_PREFECTURE_LIST = ["北海道", "青森県",...,"千葉県","東京都","神奈川県",...,"鹿児島県","沖縄県"]
prefs = ["東京都", "沖縄県", "青森県"]

# prefsをISOの都道府県リスト順に並べ替えたい

p ISO_PREFECTURE_LIST & prefs
#=> ["青森県, "東京都", "沖縄県"]

「&」は両集合の共通要素を返してくれる積演算だが(intersectionのエイリアス)、結果の集合の順番が左辺のものになるので、こういうこともできる。

ただし
「重複する要素は取り除かれます。」
なので用途に注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?