首页 > 解决方案 > Java Spring 数据:按 ID 描述排序

问题描述

我只希望我的数据按 ID Descendent 排序,但我不知道该怎么做,这是我在服务层中的代码

public Page<Facture> selectByPage(Pageable p) {
    Page<Facture> pagedResult = factureRepository.findAll(p);
    return pagedResult;
}

标签: javaeclipsespring-bootspring-data

解决方案


您可以使用Sort.of(...).descending()降序按字段排序。

public Page<Facture> selectByPage(Pageable p) {
    // Here, replace "id" with your field name that refers to id.
    Pageable pSort = PageRequest.of(p.getPageNumber(), p.getPageSize(), Sort.by("id").descending());
    
    Page<Facture> pagedResult = factureRepository.findAll(pSort);
    return pagedResult;
}

推荐阅读