Spring Boot 按属性的子字符串对对象列表进行排序

ljsrvy3e  于 7个月前  发布在  Spring
关注(0)|答案(1)|浏览(77)

我有一个包含属性date的对象列表,看起来像这样“Thu Oct 26 2023”
我想按子串(8,9)对这些对象进行排序,在本例中是26。
我查过将子字符串解析为int,然后使用Comparator和Collections.sort,但我想知道是否有更好的方法来做到这一点。

uxh89sit

uxh89sit1#

我想按子串(8,9)对这些对象进行排序,在本例中是26。
substring(8, 9)不会从"Thu Oct 26 2023"中提供26,它将提供**2**。要获得26,您需要substring(8, 10)。在这个阶段,很难说 * 月份 * 是否总是两位数格式(如010230等),或者也可能是个位数格式(如1230等)。
在提供的日期("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”,您可能会认为排序的结果是:"1", "3", "5", "10"但是...它不会是。它将被排序为"1", "10", "3", "5"。如果上面的值是实际的int或long类型的整数值:10, 3, 1, and 5,那么这些数字确实会按照预期的升序排序为1, 3, 5, 10
为了解决这个小问题,我们可以将String表示的整数值转换为BigString,BigString接受字符串值并允许正确执行排序:

// `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"
要查看上面的代码,请创建一个新项目,并使用main()方法创建一个SortByDay类,然后将以下可运行代码复制粘贴到其中。请务必阅读代码中的注解:

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

相关问题