mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复NullComparator反转无效问题
This commit is contained in:
parent
953fc7b18f
commit
8bd76de6fe
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.16.M1 (2023-03-23)
|
# 5.8.16.M1 (2023-03-26)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 改进Calculator.conversion,兼容乘法符号省略写法(issue#2964@Github)
|
* 【core 】 改进Calculator.conversion,兼容乘法符号省略写法(issue#2964@Github)
|
||||||
@ -22,6 +22,7 @@
|
|||||||
* 【core 】 修复JSONUtil.toBean可能的空指针问题(issue#2987@Github)
|
* 【core 】 修复JSONUtil.toBean可能的空指针问题(issue#2987@Github)
|
||||||
* 【core 】 修复CalendarUtil.isSameMonth没有判断公元前导致不一致的问题(issue#3011@Github)
|
* 【core 】 修复CalendarUtil.isSameMonth没有判断公元前导致不一致的问题(issue#3011@Github)
|
||||||
* 【core 】 修复WatchUtil createModify maxDepth传递后没有使用问题(issue#3005@Github)
|
* 【core 】 修复WatchUtil createModify maxDepth传递后没有使用问题(issue#3005@Github)
|
||||||
|
* 【core 】 修复NullComparator反转无效问题(pr#964@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.15 (2023-03-09)
|
# 5.8.15 (2023-03-09)
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
package cn.hutool.core.comparator;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PropertyComparatorTest {
|
||||||
|
@Test
|
||||||
|
public void sortNullTest() {
|
||||||
|
final ArrayList<User> users = ListUtil.toList(
|
||||||
|
new User("1", "d"),
|
||||||
|
new User("2", null),
|
||||||
|
new User("3", "a")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 默认null在末尾
|
||||||
|
final List<User> sortedList1 = ListUtil.sort(users, new PropertyComparator<>("b"));
|
||||||
|
Assert.assertEquals("a", sortedList1.get(0).getB());
|
||||||
|
Assert.assertEquals("d", sortedList1.get(1).getB());
|
||||||
|
Assert.assertNull(sortedList1.get(2).getB());
|
||||||
|
|
||||||
|
// null在首
|
||||||
|
final List<User> sortedList2 = ListUtil.sort(users, new PropertyComparator<>("b", false));
|
||||||
|
Assert.assertNull(sortedList2.get(0).getB());
|
||||||
|
Assert.assertEquals("a", sortedList2.get(1).getB());
|
||||||
|
Assert.assertEquals("d", sortedList2.get(2).getB());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void reversedTest() {
|
||||||
|
final ArrayList<User> users = ListUtil.toList(
|
||||||
|
new User("1", "d"),
|
||||||
|
new User("2", null),
|
||||||
|
new User("3", "a")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 反序
|
||||||
|
final List<User> sortedList = ListUtil.sort(users, new PropertyComparator<>("b").reversed());
|
||||||
|
Assert.assertNull(sortedList.get(0).getB());
|
||||||
|
Assert.assertEquals("d", sortedList.get(1).getB());
|
||||||
|
Assert.assertEquals("a", sortedList.get(2).getB());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
static class User{
|
||||||
|
private String a;
|
||||||
|
private String b;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user