我们使用Comparable接口对ArrayList进行排序但如何修改Comparable接口的CompareTo函数,以便在函数返回int时基于float字段对arraylist进行排序。
我试图按用户评级(浮动数据类型)对电影数据进行排序。下面是我创建的类,用于保存通过网络调用通过Json feed获得的不同值。
/**
* 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);
}
}
答案 0 :(得分:1)
谢谢Salivan !!此外,我试图做下面似乎它确实工作。 如果对以下内容有任何其他评论,请告诉我
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;
}