Table of Contents
ToggleJava Collections sort Method
在 Java 中對資料進行排序,提供 Collections sort 方法操作排序,預設會按升序對集合進行排序,當資料需要按特定順序排列,可以實作 Comparator 接口,使用傳統方式或 Lambda 表達式來實作其功能, Sort Java Collections 提供了預設比較器和建立自定義比較器,對資料進行排序,本篇增加了範例,並透過單元測試來驗證產出結果。
public static <T extends Comparable<? super T>> void sort(List<T> list) {
}
public static <T> void sort(List<T> list, Comparator<? super T> c) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collection
| +- sort
| +- CollectionsSortTest.java
單元測試
Collections Sort Java 提供不同類型的資料進行排序,使用預設或自定義比較器,排序集合中的元素。
sort
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照預設比較器,由小到大,升序對集合進行排序。
@Test
public void sort() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
[Orange, Mango, Peach]
[Mango, Orange, Peach]
sortWithReverseOrder
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照預設反向比較器,由大到小,降序對集合進行排序。
@Test
public void sortWithReverseOrder() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, Comparator.reverseOrder());
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
}
[[Orange, Mango, Peach]
[Peach, Orange, Mango]
sortWithComparator
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithComparator() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, (s1, s2) -> s1.length() - s2.length());
System.out.println(list);
assertThat(list).containsExactly("Mango", "Peach", "Orange");
}
[Orange, Mango, Peach]
[Mango, Peach, Orange]
sortWithComparing
Collections Sort Example 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithComparing() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, Comparator.comparing(String::length));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Peach", "Orange");
}
[Orange, Mango, Peach]
[Mango, Peach, Orange]
sortWithMultipleConditions
Collections Sort Example 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照多個自定義比較器,由小到大,升序對集合進行排序。
protected static Comparator<String> nameComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
};
protected static Comparator<String> lengthComparator = (s1, s2) -> Integer.compare(s1.length(), s2.length());
@Test
public void sortWithMultipleConditions() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange");
System.out.println(list);
Collections.sort(list, nameComparator.thenComparing(lengthComparator));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
[Mango, Peach, Orange]
[Mango, Orange, Peach]
sortWithNull
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照自定義比較器, null 元素放在第一個,由小到大,升序對集合進行排序。
@Test
public void sortWithNull() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, (s1, s2) -> {
if (s1 == null) {
return s2 == null ? 0 : -1;
} else if (s2 == null) {
return 1;
}
return s1.compareTo(s2);
});
System.out.println(list);
assertThat(list).containsExactly(null, "Mango", "Orange", "Peach");
}
[Mango, Peach, Orange, null]
[null, Mango, Orange, Peach]
sortWithNullsFirst
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照 nullsFirst 比較器, null 元素放在第一個,由小到大,升序對集合進行排序。
@Test
public void sortWithNullsFirst() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(null, "Mango", "Orange", "Peach");
}
[Mango, Peach, Orange, null]
[null, Mango, Orange, Peach]
sortWithNullsLast
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照 nullsLast 比較器, null 元素放在最後個,由小到大,升序對集合進行排序。
@Test
public void sortWithNullsLast() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, Comparator.nullsLast(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach", null);
}
[Mango, Peach, Orange, null]
[Mango, Orange, Peach, null]
CollectionsSortTest.java
Collections Sort Example 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.collection.sort;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
public class CollectionsSortTest {
@Test
public void sort() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
@Test
public void sortWithReverseOrder() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, Comparator.reverseOrder());
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
}
@Test
public void sortWithComparator() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, (s1, s2) -> s1.length() - s2.length());
System.out.println(list);
assertThat(list).containsExactly("Mango", "Peach", "Orange");
}
@Test
public void sortWithComparing() {
List<String> list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list, Comparator.comparing(String::length));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Peach", "Orange");
}
protected static Comparator<String> nameComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
};
protected static Comparator<String> lengthComparator = (s1, s2) -> Integer.compare(s1.length(), s2.length());
@Test
public void sortWithMultipleConditions() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange");
System.out.println(list);
Collections.sort(list, nameComparator.thenComparing(lengthComparator));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
@Test
public void sortWithNull() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, (s1, s2) -> {
if (s1 == null) {
return s2 == null ? 0 : -1;
} else if (s2 == null) {
return 1;
}
return s1.compareTo(s2);
});
System.out.println(list);
assertThat(list).containsExactly(null, "Mango", "Orange", "Peach");
}
@Test
public void sortWithNullsFirst() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(null, "Mango", "Orange", "Peach");
}
@Test
public void sortWithNullsLast() {
List<String> list = Arrays.asList("Mango", "Peach", "Orange", null);
System.out.println(list);
Collections.sort(list, Comparator.nullsLast(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach", null);
}
}
心得分享
Java Collections Sort 排序是指使用特定標準以特定順序排列資料,可以對不同類型的資料進行排序,像是字串、數字和物件,提供了幾種 Sort 常見方法的操作範例,在應用上相當廣泛,熟悉 Collections Sort Example 這些方法的操作,可以讓排序依照不同的需求,更簡易、快速地實現功能。