排序列表升序和降序android java

时间:2016-10-27 19:54:47

标签: java android

我有一个class Users::RegistrationsController < Devise::RegistrationsController # before_action :configure_sign_up_params, only: [:create] # before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up # def new # super # end # POST /resource #def create # super #end # GET /resource/edit # def edit # super # end # PUT /resource #def update # super #end # DELETE /resource # def destroy # super # end # GET /resource/cancel # Forces the session data which is usually expired after sign # in to be expired now. This is useful if the user wants to # cancel oauth signing in/up in the middle of the process, # removing all OAuth session data. # def cancel # super # end # protected # If you have extra params to permit, append them to the sanitizer. # def configure_sign_up_params # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) # end # If you have extra params to permit, append them to the sanitizer. # def configure_account_update_params # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) # end # The path used after sign up. # def after_sign_up_path_for(resource) # super(resource) # end # The path used after sign up for inactive accounts. # def after_inactive_sign_up_path_for(resource) # super(resource) # end end 包含数据。如何将其分类为升序和降序?

List<>

这是我的数据类

List<Data> data = new ArrayList<>();

        data.add(new Data("Batman vs Superman", "Following the destruction of Metropolis, Batman embarks on a personal vendetta against Superman ", R.drawable.ic_action_movie));
        data.add(new Data("X-Men: Apocalypse", "X-Men: Apocalypse is an upcoming American superhero film based on the X-Men characters that appear in Marvel Comics ", R.drawable.ic_action_movie));
        data.add(new Data("Captain America: Civil War", "A feud between Captain America and Iron Man leaves the Avengers in turmoil.  ", R.drawable.ic_action_movie));
        data.add(new Data("Kung Fu Panda 3", "After reuniting with his long-lost father, Po  must train a village of pandas", R.drawable.ic_action_movie));
        data.add(new Data("Warcraft", "Fleeing their dying home to colonize another, fearsome orc warriors invade the peaceful realm of Azeroth. ", R.drawable.ic_action_movie));
        data.add(new Data("Alice in Wonderland", "Alice in Wonderland: Through the Looking Glass ", R.drawable.ic_action_movie));

2 个答案:

答案 0 :(得分:1)

让你的类实现Comparable接口,这可以让Java处理大部分繁重的工作。

public class Data implements Comparable<Data>{
    @Override
    public int compareTo(Data another) {
        return this.title.compareTo(another.title);
        // can also compareToIgnoreCase if not case sensitive
    }
}

然后只需致电Collections.sort(data);

这将根据Data类中的compareTo函数对数据进行排序。

答案 1 :(得分:-1)

您可以使用Java8,如下所示:

&#xA;&#xA;

data.sort((数据d1,数据d2) - > d1.title.compareTo(d2。标题));

&#xA;&#xA;

在您的代码中,我建议不要直接使用'public'公开类字段,而是使用getter和setter(封装字段) )。

&#xA;&#xA;

data.sort((数据d1,数据d2) - > d1.getTitle()。compareTo(d2.getTitle())); < /强>

&#XA;