我一直在寻找和try 各种事情,以获得时间戳排序从最低的数字到最高的.我用的是postgres和spring boot 3.2.2.我遇到的问题是startTimestamp是作为字符串存储在数据库中,我认为这就是为什么默认排序不正确.我也试过Order.asc("startTimeStamp"),但postgres也发送类似的响应.

这是我的实体

public class YoutubeVideo {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID Id;
    private String videoId;
    private String startTimeStamp;
    private String fullYouTubeUrl;

我的服务中有这个函数,可以从JpaRepository中提取数据

 public Page<YoutubeVideoDto> findAllByYtVideoId(Integer pageNum, String videoId) {
                Page<YoutubeVideo> dbResponse = youtubeVideoRepository
                                .findAllByYtVideoId(PageRequest.of(pageNum, NUMBER_OF_ITEMS_PER_PAGE, Sort.by("startTimeStamp").ascending()), videoId);
                                                                

                return dbResponse.map(this::mapYoutubeVideoDto);

        };

JPA存储库

public interface YoutubeVideoRepository extends JpaRepository<YoutubeVideo, UUID> {

    Page<YoutubeVideo> findAllByYtVideoId(Pageable pageable, String videoId);

}

我try 使用Sort.by("startTimeStamp").ascending,但这是我请求API时得到的结果.

{
    "content": [
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "938",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=938",
            "id": "1423cb48-2bb4-42e3-a6a9-78b5b5995198"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "620",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=620",
            "id": "d97ccc9e-5aed-47ae-a137-9ab2033b437e"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "6023",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=6023",
            "id": "73517417-6d62-4511-9e96-68a0232d6999"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "5788",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=5788",
            "id": "69960697-6162-4214-9f2f-b3f787f86989"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "5460",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=5460",
            "id": "f7446385-4802-4666-8021-ed75de826a5f"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "451",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=451",
            "id": "70ed415f-23f8-4ea3-be1a-4b6bfeb83ce0"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3979",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3979",
            "id": "51634c42-503f-4c49-9d5e-b4e3bebc3861"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3679",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3679",
            "id": "e13419be-ed68-4995-9212-65e6428f613a"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3421",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3421",
            "id": "6826b818-73ac-4410-9a61-cafa38c46f92"
        },
        {
            "videoId": "videoIdHere",
            "startTimeStamp": "3126",
            "fullYouTubeUrl": "https://www.youtube.com/watch?v=videoIdHere&t=3126",
            "id": "359a55e9-e2e1-4300-b576-4613b70b2a93"
        }
    ],
    "pageable": {
        "pageNumber": 0,
        "pageSize": 10,
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "paged": true,
        "unpaged": false
    },
    "totalPages": 2,
    "totalElements": 15,
    "last": false,
    "first": true,
    "size": 10,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "numberOfElements": 10,
    "empty": false
}

StartTimeStamp不是从最低到最高的顺序.

我在网上找了看,在这里也是堆积如山,但找不到我想要的东西.我怀疑我需要将startTimestamp转换为整数,然后使用默认排序器?我想需要实现一个定制的分类器?或者有没有人有我监督的其他解决方案.

谢谢.

推荐答案

问题在于,时间戳是以字符串形式存储的,排序将按词典顺序而不是数字顺序进行.如果您只在此字段中存储整数,我建议将类型更改为Integer.

但是,如果无法更改该字段的类型,则可以编写一个自定义查询,该查询将时间戳转换为整数以进行排序:

@Query("SELECT v FROM YoutubeVideo v WHERE v.videoId = :videoId ORDER BY CAST(v.startTimeStamp AS int) ASC")
Page<YoutubeVideo> findByVideoIdSortedByStartTimeStamp(String videoId, Pageable pageable);

Java相关问答推荐

CriteriaQuery with max

使用联接和分页的SpringBoot Spring数据JPA

在模拟超类中设置非setter属性的值

如何使用AWS CLI从S3存储桶中的所有对象中删除用户定义的元数据?

';com.itextpdf.ext.html.WebColors已弃用

只需最少的代码更改即可将版本号标记添加到日志(log)

名称冲突具有相同的擦除

相同的Java SerializedLambda为implMethodKind返回不同的结果

Java编译器抛出可能未正确初始化的错误?

基于接口的投影、原生查询和枚举

为什么同步数据块无效?

支持MySQL 5.6的最新Hibernate版本

SpringBoot:在条件{Variable}.isBlank/{Variable}.isEmpty不起作用的情况下进行路径变量验证

如果List是一个抽象接口,那么Collectors.toList()如何处理流呢?

如果第一位数字和最后一位数字相差超过一位,您将如何获得随机数?

JOLT根据值删除并保留其余的json键

如何对存储为字符串的大数字数组进行排序?

在Spring Boot中使用咖啡因进行缓存-根据输出控制缓存

为什么Instant没有从UTC转换为PostgreSQL的时区?

获取月份';s在java中非UTC时区的开始时间和结束时间