020-001-014
Collections.sort()とComparator
中級
問題説明
Collections.sort()とComparator
この問題では: Collections.sort() に Comparator を指定して、ArrayList の要素をカスタム順序でソートするプログラムを作成します。
学習目標: Collections.sort()とComparatorによるカスタムソートを理解する
概要
Collections.sort() はリストを自然順序でソートしますが、Comparator を第2引数に渡すとカスタム順序でソートできます。
仕様
ArrayList<String>に"banana","apple","cherry","date"を追加- 自然順序(アルファベット順)でソートして出力
- 文字列の長さの昇順でソートして出力
- 文字列の長さの降順でソートして出力
出力形式
Alphabetical: [apple, banana, cherry, date]
By length: [date, apple, banana, cherry]
By length desc: [banana, cherry, apple, date]
