提问人:Miguel Celos 提问时间:11/8/2023 更新时间:11/8/2023 访问量:41
按属性的子字符串对对象列表进行排序
Sorting a list of objects by an attribute's substring
问:
我有一个对象列表,其中包含如下所示的属性日期:“Thu Oct 26, 2023”
我想按子字符串(8,9)对这些对象进行排序,在本例中为26。
我已经查找了将子字符串解析为 int 并使用 Comparator 和 Collections.sort 的问题,但我想知道是否有更好的方法可以做到这一点。
答:
我想按它们的子字符串(8,9)对这些对象进行排序,该子字符串为26 这种情况。
substring(8, 9)
不会提供 26 个 ,它将提供 2
。要得到你需要.在这个阶段,很难说月份中的天数是否总是两位数的格式(如、、等),或者也可能是个位数的格式(如、、等)。"Thu Oct 26 2023"
26
substring(8, 10)
01
02
30
1
2
30
在提供的日期()中,可以肯定的是,如果始终遵循实际的字符串日期格式,则日期组件之间将始终存在空格。基于这一事实,拆分日期组件以收集该月的实际日期会更有益,而不管它有多少位数。"Thu Oct 26 2023"
// `hireDate` is an instance variable within the `MyObject` class:
// `list` is a List of MyObject instances, ex: List<MyObject> list = new ArrayList<>();
list.sort(java.util.Comparator.comparing(e -> e.hireDate.split("\\s")[2]));
但是,这不会正确排序。考虑到日期是一个字符串,并且字符串表示整数(例如月份中的某一天和年份)的排序方式与实际的整数(int 或 long 类型)不同,例如,要对以下字符串值进行排序:“10”、“3”、“1”和“5”,您会认为排序结果为: 但。。。不会的。它将被排序为 .如果上述值是实际的 int 或 long 类型整数值:则这些数字确实会按预期的升序排序。"1", "3", "5", "10"
"1", "10", "3", "5"
10, 3, 1, and 5
1, 3, 5, 10
为了解决这个小问题,我们可以将 String 表示的整数值转换为 BigInteger,它接受字符串值并允许正确执行排序:
// `hireDate` is an instance variable within the `MyObject` class:
// `list` is a List of MyObject instances, ex: List<MyObject> list = new ArrayList<>();
list.sort(java.util.Comparator.comparing(e ->
new java.math.BigInteger(e.hireDate.split("\\s")[2])));
将导致以下排序列表: 。"1", "3", "5", "10"
若要查看上述代码的运行情况,请创建一个新项目,并使用方法创建一个 SortByDay 类,并将以下可运行代码复制粘贴到其中。请务必阅读代码中的注释:main()
public class SortByDay {
public static void main(String[] args) {
/* App started this way to avoid the need for statics
unless we really want them: */
new SortByDay().startApp(args);
}
private void startApp(String[] args) {
// Create a java.util.List of MyObject instances:
java.util.List<MyObject> objList = new java.util.ArrayList<>();
objList.add(new MyObject("Tom Jones", 66, "Thu Oct 4 2023", 32.50));
objList.add(new MyObject("Tracey Johnson", 59, "Tue Oct 10 2023", 31.00));
objList.add(new MyObject("Dave Simpson", 30, "Wed Oct 1 2023", 43.75));
/* Display the List before sorting in Ascending order
by day of month: */
System.out.println("Before Sorting:\n===============");
for (MyObject o : objList) {
System.out.println(o.toString());
}
System.out.println();
/* Sort the List of MyObject based on the day of month
in each instance contained within the List: */
sortListedObjectByDayOfMonth(objList);
/* Display the List after sorting in Ascending order
by day of month: */
System.out.println("After Sorting:\n==============");
for (MyObject o : objList) {
System.out.println(o.toString());
}
}
public void sortListedObjectByDayOfMonth(java.util.List<MyObject> list) {
list.sort(java.util.Comparator.comparing(e ->
new java.math.BigInteger(e.hireDate.split("\\s")[2])));
}
public class MyObject {
// Instance (member) variables:
private String name;
private int age;
private String hireDate;
private double wage;
// Constructor:
public MyObject(String name, int age, String hireDate, double wage) {
this.name = name;
this.age = age;
this.hireDate = hireDate;
this.wage = wage;
}
@Override
public String toString() {
return name + ", " + age + ", \"" + hireDate + "\", $"
+ String.format("%.02f", wage);
}
}
}
运行代码时,控制台窗口输出应如下所示:
Before Sorting:
===============
Tom Jones, 66, "Thu Oct 4 2023", $32.50
Tracey Johnson, 59, "Tue Oct 10 2023", $31.00
Dave Simpson, 30, "Wed Oct 1 2023", $43.75
After Sorting:
==============
Dave Simpson, 30, "Wed Oct 1 2023", $43.75
Tom Jones, 66, "Thu Oct 4 2023", $32.50
Tracey Johnson, 59, "Tue Oct 10 2023", $31.00
评论
String
LocalDate
list.sort(Comparator.comparing(e -> e.attr.substring(8, 9)));