我一直在寻找和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
转换为整数,然后使用默认排序器?我想需要实现一个定制的分类器?或者有没有人有我监督的其他解决方案.
谢谢.