1
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 3 years have passed since last update.

文字列配列を長さ&五十音順にソートする

Last updated at Posted at 2020-09-18

はじめに

文字列でできた配列を単語の長さでソートし,かつ五十音順にしたいケースがあったのでメモとして残す。

解決方法

※ いただいたコメントを元に修正しました。shiracamusさんありがとうございます!!

words = ["にほん", "あめりか", "ろしあ", "ふらんす", "いたりあ", "ちゅうごく", "しんがぽーる", "おーすとらりあ", "たい", "ちり", "どいつ", "いぎりす"]
words = sorted(words, key=lambda word: (len(word), word))
print(words)

出力結果は次のようになります:

['たい', 'ちり', 'どいつ', 'にほん', 'ろしあ', 'あめりか', 'いぎりす', 'いたりあ', 'ふらんす', 'ちゅうごく', 'しんがぽーる', 'おーすとらりあ']

簡単ですね。
もっと良い方法があれば教えてください。

1
0
3

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
1
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?