「Java8」Collections.sortでリストの要素を昇順、降順になるサンプル

環境
JavaSE 1.8
Eclipse IDE 2019-12

書式
public static <T extends Comparable<? super T>> void sort(List<T> list)
指定されたリストを、その要素の自然順序付けに従って昇順にソートします。
リストのすべての要素は、Comparableインタフェースを実装する必要があります。
型パラメータ:
T – リスト内のオブジェクトのクラス
パラメータ:
list – ソートされるリスト。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionDemo {
public static void main(String[] args) {
System.out.println("CollectionsでDoubleリストの要素をソートする");
List<Double> lstD = new ArrayList<Double>();
Collections.addAll(lstD, 1.3, 4.2, 5.1, 6.5, 7.4);
Collections.sort(lstD);
System.out.println("Double配列要素の昇順になる結果1:\n" + lstD.toString());
Collections.sort(lstD, Comparator.reverseOrder());
System.out.println("Double配列要素の降順になる結果1:\n" + lstD.toString());
System.out.println("\nCollectionsでIntegerリストの要素をソートする");
List<Integer> lstIt = new ArrayList<Integer>();
Collections.addAll(lstIt, 15, 42, 67, 86, 38);
Collections.sort(lstIt);
System.out.println("Integer配列要素の昇順になる結果1:\n" + lstIt.toString());
Collections.sort(lstIt, Comparator.reverseOrder());
System.out.println("Integer配列要素の降順になる結果2:\n" + lstIt.toString());
}
}
package com.arkgame.study; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CollectionDemo { public static void main(String[] args) { System.out.println("CollectionsでDoubleリストの要素をソートする"); List<Double> lstD = new ArrayList<Double>(); Collections.addAll(lstD, 1.3, 4.2, 5.1, 6.5, 7.4); Collections.sort(lstD); System.out.println("Double配列要素の昇順になる結果1:\n" + lstD.toString()); Collections.sort(lstD, Comparator.reverseOrder()); System.out.println("Double配列要素の降順になる結果1:\n" + lstD.toString()); System.out.println("\nCollectionsでIntegerリストの要素をソートする"); List<Integer> lstIt = new ArrayList<Integer>(); Collections.addAll(lstIt, 15, 42, 67, 86, 38); Collections.sort(lstIt); System.out.println("Integer配列要素の昇順になる結果1:\n" + lstIt.toString()); Collections.sort(lstIt, Comparator.reverseOrder()); System.out.println("Integer配列要素の降順になる結果2:\n" + lstIt.toString()); } }
package com.arkgame.study;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionDemo {

      public static void main(String[] args) {
            System.out.println("CollectionsでDoubleリストの要素をソートする");
            List<Double> lstD = new ArrayList<Double>();
            Collections.addAll(lstD, 1.3, 4.2, 5.1, 6.5, 7.4);
            Collections.sort(lstD);
            System.out.println("Double配列要素の昇順になる結果1:\n" + lstD.toString());
            Collections.sort(lstD, Comparator.reverseOrder());
            System.out.println("Double配列要素の降順になる結果1:\n" + lstD.toString());

            System.out.println("\nCollectionsでIntegerリストの要素をソートする");
            List<Integer> lstIt = new ArrayList<Integer>();
            Collections.addAll(lstIt, 15, 42, 67, 86, 38);
            Collections.sort(lstIt);
            System.out.println("Integer配列要素の昇順になる結果1:\n" + lstIt.toString());
            Collections.sort(lstIt, Comparator.reverseOrder());
            System.out.println("Integer配列要素の降順になる結果2:\n" + lstIt.toString());

      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
CollectionsでDoubleリストの要素をソートする
Double配列要素の昇順になる結果1:
[1.3, 4.2, 5.1, 6.5, 7.4]
Double配列要素の降順になる結果1:
[7.4, 6.5, 5.1, 4.2, 1.3]
CollectionsでIntegerリストの要素をソートする
Integer配列要素の昇順になる結果1:
[15, 38, 42, 67, 86]
Integer配列要素の降順になる結果2:
[86, 67, 42, 38, 15]
CollectionsでDoubleリストの要素をソートする Double配列要素の昇順になる結果1: [1.3, 4.2, 5.1, 6.5, 7.4] Double配列要素の降順になる結果1: [7.4, 6.5, 5.1, 4.2, 1.3] CollectionsでIntegerリストの要素をソートする Integer配列要素の昇順になる結果1: [15, 38, 42, 67, 86] Integer配列要素の降順になる結果2: [86, 67, 42, 38, 15]
CollectionsでDoubleリストの要素をソートする
Double配列要素の昇順になる結果1:
[1.3, 4.2, 5.1, 6.5, 7.4]
Double配列要素の降順になる結果1:
[7.4, 6.5, 5.1, 4.2, 1.3]

CollectionsでIntegerリストの要素をソートする
Integer配列要素の昇順になる結果1:
[15, 38, 42, 67, 86]
Integer配列要素の降順になる結果2:
[86, 67, 42, 38, 15]

 

Java

Posted by arkgame