如何根据浮点字段对自定义 ArrayList 进行排序
How to sort custom ArrayList based on a float field
我们使用 Comparable 接口对 ArrayList 进行排序,但是我如何修改 Comparable 接口的 CompareTo 函数以根据 float 字段对 arraylist 进行排序作为函数 returns int。
我试图按用户评分(浮点数据类型)对电影数据进行排序。下面是我的 class,我创建它是为了保存通过网络调用的 Json 提要获得的不同值。
/**
* Created by anant on 2015-08-19.
*/
public class MoviesDetail implements Parcelable, Comparable<MoviesDetail> {
private String jsf_original_title;
private String jsf_poster;
private String jsf_plot_synopsis;
private float jsf_user_rating;
private String jsf_release_date;
private String jsf_results;
private int jsf_id;
public MoviesDetail() {
}
private MoviesDetail(Parcel p) {
jsf_original_title = p.readString();
jsf_poster = p.readString();
jsf_plot_synopsis = p.readString();
jsf_user_rating = p.readFloat();
jsf_release_date = p.readString();
jsf_results = p.readString();
jsf_id = p.readInt();
}
public String getJsf_original_title() {
return jsf_original_title;
}
public String getJsf_poster() {
return jsf_poster;
}
public String getJsf_plot_synopsis() {
return jsf_plot_synopsis;
}
public float getJsf_user_rating() {
return jsf_user_rating;
}
public String getJsf_release_date() {
return jsf_release_date;
}
public String getJsf_results() {
return jsf_results;
}
public int getJsf_id() {
return jsf_id;
}
public void setJsf_original_title(String jsf_original_title) {
this.jsf_original_title = jsf_original_title;
}
public void setJsf_poster(String jsf_poster) {
this.jsf_poster = jsf_poster;
}
public void setJsf_plot_synopsis(String jsf_plot_synopsis) {
this.jsf_plot_synopsis = jsf_plot_synopsis;
}
public void setJsf_user_rating(float jsf_user_rating) {
this.jsf_user_rating = jsf_user_rating;
}
public void setJsf_release_date(String jsf_release_date) {
this.jsf_release_date = jsf_release_date;
}
public void setJsf_results(String jsf_results) {
this.jsf_results = jsf_results;
}
public void setJsf_id(int jsf_id) {
this.jsf_id = jsf_id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(jsf_original_title);
dest.writeString(jsf_poster);
dest.writeString(jsf_plot_synopsis);
dest.writeFloat(jsf_user_rating);
dest.writeString(jsf_release_date);
dest.writeString(jsf_results);
dest.writeInt(jsf_id);
}
public final Parcelable.Creator<MoviesDetail> CREATOR = new Parcelable.Creator<MoviesDetail>() {
/**
* Create a new instance of the Parcelable class, instantiating it
* from the given Parcel whose data had previously been written by
* {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
*
* @param source The Parcel to read the object's data from.
* @return Returns a new instance of the Parcelable class.
*/
@Override
public MoviesDetail createFromParcel(Parcel source) {
return new MoviesDetail(source);
}
/**
* Create a new array of the Parcelable class.
*
* @param size Size of the array.
* @return Returns an array of the Parcelable class, with every entry
* initialized to null.
*/
@Override
public MoviesDetail[] newArray(int size) {
return new MoviesDetail[0];
}
};
@Override
public int compareTo(MoviesDetail aMovie) {
int i = (int) aMovie.getJsf_user_rating();
return getJsf_user_rating().compareTo(i);
}
}
谢谢萨利文!!此外,我正在尝试执行以下操作,但似乎确实有效。
如果对以下内容有任何其他评论,请告诉我
public int compareTo(MoviesDetail aMovie) {
// int i = (int)another.getJsf_user_rating();
if(aMovie.getJsf_user_rating()<this.getJsf_user_rating()){
return -1;
}
else if(aMovie.getJsf_user_rating()==this.getJsf_user_rating()){
return 0;
}
return 1;
}
我们使用 Comparable 接口对 ArrayList 进行排序,但是我如何修改 Comparable 接口的 CompareTo 函数以根据 float 字段对 arraylist 进行排序作为函数 returns int。
我试图按用户评分(浮点数据类型)对电影数据进行排序。下面是我的 class,我创建它是为了保存通过网络调用的 Json 提要获得的不同值。
/**
* Created by anant on 2015-08-19.
*/
public class MoviesDetail implements Parcelable, Comparable<MoviesDetail> {
private String jsf_original_title;
private String jsf_poster;
private String jsf_plot_synopsis;
private float jsf_user_rating;
private String jsf_release_date;
private String jsf_results;
private int jsf_id;
public MoviesDetail() {
}
private MoviesDetail(Parcel p) {
jsf_original_title = p.readString();
jsf_poster = p.readString();
jsf_plot_synopsis = p.readString();
jsf_user_rating = p.readFloat();
jsf_release_date = p.readString();
jsf_results = p.readString();
jsf_id = p.readInt();
}
public String getJsf_original_title() {
return jsf_original_title;
}
public String getJsf_poster() {
return jsf_poster;
}
public String getJsf_plot_synopsis() {
return jsf_plot_synopsis;
}
public float getJsf_user_rating() {
return jsf_user_rating;
}
public String getJsf_release_date() {
return jsf_release_date;
}
public String getJsf_results() {
return jsf_results;
}
public int getJsf_id() {
return jsf_id;
}
public void setJsf_original_title(String jsf_original_title) {
this.jsf_original_title = jsf_original_title;
}
public void setJsf_poster(String jsf_poster) {
this.jsf_poster = jsf_poster;
}
public void setJsf_plot_synopsis(String jsf_plot_synopsis) {
this.jsf_plot_synopsis = jsf_plot_synopsis;
}
public void setJsf_user_rating(float jsf_user_rating) {
this.jsf_user_rating = jsf_user_rating;
}
public void setJsf_release_date(String jsf_release_date) {
this.jsf_release_date = jsf_release_date;
}
public void setJsf_results(String jsf_results) {
this.jsf_results = jsf_results;
}
public void setJsf_id(int jsf_id) {
this.jsf_id = jsf_id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(jsf_original_title);
dest.writeString(jsf_poster);
dest.writeString(jsf_plot_synopsis);
dest.writeFloat(jsf_user_rating);
dest.writeString(jsf_release_date);
dest.writeString(jsf_results);
dest.writeInt(jsf_id);
}
public final Parcelable.Creator<MoviesDetail> CREATOR = new Parcelable.Creator<MoviesDetail>() {
/**
* Create a new instance of the Parcelable class, instantiating it
* from the given Parcel whose data had previously been written by
* {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
*
* @param source The Parcel to read the object's data from.
* @return Returns a new instance of the Parcelable class.
*/
@Override
public MoviesDetail createFromParcel(Parcel source) {
return new MoviesDetail(source);
}
/**
* Create a new array of the Parcelable class.
*
* @param size Size of the array.
* @return Returns an array of the Parcelable class, with every entry
* initialized to null.
*/
@Override
public MoviesDetail[] newArray(int size) {
return new MoviesDetail[0];
}
};
@Override
public int compareTo(MoviesDetail aMovie) {
int i = (int) aMovie.getJsf_user_rating();
return getJsf_user_rating().compareTo(i);
}
}
谢谢萨利文!!此外,我正在尝试执行以下操作,但似乎确实有效。 如果对以下内容有任何其他评论,请告诉我
public int compareTo(MoviesDetail aMovie) {
// int i = (int)another.getJsf_user_rating();
if(aMovie.getJsf_user_rating()<this.getJsf_user_rating()){
return -1;
}
else if(aMovie.getJsf_user_rating()==this.getJsf_user_rating()){
return 0;
}
return 1;
}