Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 通过用户界面手动对列进行排序_Hibernate_Sorting - Fatal编程技术网

Hibernate 通过用户界面手动对列进行排序

Hibernate 通过用户界面手动对列进行排序,hibernate,sorting,Hibernate,Sorting,我正在使用hibernate从数据库中检索数据。我的UI在表中显示不同的列。我想在列上实现排序功能 触发图标时,名称应按A-Z然后Z-A的顺序排序 请在这方面帮助我。您可以使用比较器对数据进行排序(假设您不想点击数据库)。假设您有一个显示在UI上的类别实例列表(类别列表): class Category{ private Long id; private String categoryName; private String otherProperty; } 现在,您可以使用Comparato

我正在使用hibernate从数据库中检索数据。我的UI在表中显示不同的列。我想在列上实现排序功能

触发图标时,名称应按A-Z然后Z-A的顺序排序


请在这方面帮助我。

您可以使用
比较器对数据进行排序(假设您不想点击数据库)。假设您有一个显示在UI上的
类别
实例列表(
类别列表
):

class Category{
private Long id;
private String categoryName;
private String otherProperty;
}
现在,您可以使用
Comparator
界面定义自定义策略,对
类别列表中的
类别
实例进行排序:

class StartegyOne implements Comparator<Category> {

    @Override
    public int compare(Category c1, Categoryc2) {
        return c1.getCategoryName().compareTo(c2.getCategoryName());
    }

}

您可以考虑在私有静态最终字段中存储此代码>策略类的实例,并重用它:

/*This is the class that receives the sort action*/
class SortActionHandler{

private static final Comparator<Category> CATEGORY_ORDER = 
                                        new Comparator<Category>() {
            public int compare(Category c1, Categoryc2) {
                return c1.getCategoryName().compareTo(c2.getCategoryName());
            }
    };

//call this method to sort your list according to category names
private void sortList(List<Category> categoryList){
Collections.sort(categoryList, CATEGORY_ORDER);
}
}
/*这是接收排序操作的类*/
类SortActionHandler{
专用静态最终比较器类别\u顺序=
新比较器(){
公共整数比较(类别c1,类别C2){
返回c1.getCategoryName().compareTo(c2.getCategoryName());
}
};
//调用此方法根据类别名称对列表进行排序
私有无效分类列表(列表类别列表){
Collections.sort(categoryList,CATEGORY_ORDER);
}
}

是要对内存中的数据进行排序,还是要点击数据库?@debojisaikia,这些元素仅从数据库中检索。但您要对已从数据库中加载的数据进行排序。。是吗?是的,是的,数据已经加载了。我不想再使用任何接口,需要现有文件的解决方案。我有model
Category
然后它的
DAOImpl
文件,DAOImpl和Category模型中的更改将被欢迎单击UI上的排序按钮,您将代码提交给某个类。。正确的?那门课有什么变化吗?@Debojit。。是的,没关系。
/*This is the class that receives the sort action*/
class SortActionHandler{

private static final Comparator<Category> CATEGORY_ORDER = 
                                        new Comparator<Category>() {
            public int compare(Category c1, Categoryc2) {
                return c1.getCategoryName().compareTo(c2.getCategoryName());
            }
    };

//call this method to sort your list according to category names
private void sortList(List<Category> categoryList){
Collections.sort(categoryList, CATEGORY_ORDER);
}
}