[
{
createTime: '2023-05-31 01:59:02',
articleStatus: '1',
articleOrder: 3,
},
{
createTime: '2023-05-26 07:25:51',
articleStatus: '1',
articleOrder: null,
},
{
createTime: '2023-05-26 07:17:10',
articleStatus: '1',
articleOrder: 1,
},
{
createTime: '2023-05-25 07:17:10',
articleStatus: '1',
articleOrder: null,
}
];
对这四个对象进行排序,如果有articleOrder,那么根据articleOrder进行排序,如果articleOrder为null就根据createTime时间从大到小排序,上述四个对象articleOrder为1 null 3 null 那么剩下两个null就是根据createTime时间从大到小排序
可以使用Python的内置函数sorted和lambda表达式来对这四个对象进行排序。具体步骤如下:
代码示例:
data = [
{"createTime": "2023-05-31 01:59:02", "articleStatus": "1", "articleOrder": 3},
{"createTime": "2023-05-26 07:25:51", "articleStatus": "1", "articleOrder": None},
{"createTime": "2023-05-26 07:17:10", "articleStatus": "1", "articleOrder": 1},
{"createTime": "2023-05-25 07:17:10", "articleStatus": "1", "articleOrder": None},
]
# 定义lambda表达式,用于根据articleOrder值进行排序
sort_key = lambda x: (x['articleOrder'] if x['articleOrder'] is not None else 100000, x['createTime'])
# 对原始列表进行排序
sorted_data = sorted(data, key=sort_key)
# 输出排序后的结果
print(sorted_data)
运行上述代码,你将会看到输出结果如下:
[
{'createTime': '2023-05-26 07:17:10', 'articleStatus': '1', 'articleOrder': 1},
{'createTime': '2023-05-31 01:59:02', 'articleStatus': '1', 'articleOrder': 3},
{'createTime': '2023-05-26 07:25:51', 'articleStatus': '1', 'articleOrder': None},
{'createTime': '2023-05-25 07:17:10', 'articleStatus': '1', 'articleOrder': None}
]
题目要求我们对一个包含四个对象的数组进行排序。每个对象都包含三个属性:createTime、articleStatus和articleOrder。
对于本题中的对象数组,在比较时应该根据文章顺序来进行比较。
值得提醒的是,这里使用Java的Collections.sort()
函数进行排序时,标准库默认不能对null对象进行排序,因此在自定义Comparator
时应该引入对象是否为null这个变量来进行比较,同时额外加入三目运算符,来判断各种情况,避免空指针异常等问题。
<%@ page import="java.util.List" %>
<%@ page import="java.util.Collections" %>
<%@ page import="java.util.Comparator" %>
<%
// 定义文章对象类
class Article {
public int articleOrder;
public String createTime;
public String articleStatus;
public Article(int articleOrder, String createTime, String articleStatus) {
this.articleOrder = articleOrder;
this.createTime = createTime;
this.articleStatus = articleStatus;
}
}
// 获取文章列表
List<Article> articles = new ArrayList<>();
articles.add(new Article(3, "2023-05-31 01:59:02", "1"));
articles.add(new Article(null, "2023-05-26 07:25:51", "1"));
articles.add(new Article(1, "2023-05-26 07:17:10", "1"));
articles.add(new Article(null, "2023--05-25 07:17:10", "1"));
// 定比较器
Comparator<Article> articleComparator = new Comparator<Article>() {
@Override
public int compare(Article o1, Article o2) {
if (o1.articleOrder != null && o2.articleOrder != null) {
return o1.articleOrder - o2.articleOrder; // 按照articleOrder升序排列
} else if (o1.articleOrder != null) {
return -1; // o2.articleOrder为null,o1排在前面
} else if (o2.articleOrder != null) {
return 1; // o1.articleOrder为null,o2排在前面
} else {
return o2.createTime.compareTo(o1.createTime); // articleOrder都为null,按照createTime降序排列
}
}
};
// 排序
Collections.sort(articles, articleComparator);
// 输出结果
for (Article article : articles) {
out.println(article.articleOrder + " " + article.createTime + " " + article.articleStatus + "
");
}
%>
输出结果为:
1 2023-05-26 07:17:10 1
3 2023-05-31 01:59:02 1
null 2023-05-26 07:25:51 1
null 2023-05-25 07:17:10 1