简体   繁体   English

如何基于浮点字段对自定义ArrayList进行排序

[英]How to sort custom ArrayList based on a float field

We use Comparable interface to sort the ArrayList but How do i modify the CompareTo function of the Comparable interface to sort the arraylist based on float field as the function returns int. 我们使用Comparable接口对ArrayList进行排序,但是随着函数返回int,我如何修改Comparable接口的CompareTo函数以基于float字段对arraylist进行排序。

I was trying to sort the movies data by their user-rating(float data type) . 我试图按它们的用户评分(浮动数据类型)对电影数据进行排序。 Below is my class which i created to hold the different values obtained via Json feed from a network call. 下面是我创建的类,用于保存通过网络调用通过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);
   }
}

Thanks Salivan!! 谢谢萨利文! Also i was trying to do the below seems it did work out. 我也试图做下面似乎确实解决了。 Let me know if any additional comments on the below 让我知道下面是否有其他意见

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM