接口Comparable实现List排序

package com.haso.bscs.view;

import java.io.Serializable;
import java.util.List;

/**
 * 产品信息view 显示
 *
 * @author sun
 *
 */
public class ProductInfoView implements Serializable,Comparable<ProductInfoView> {
 /**
  *
  */
 private static final long serialVersionUID = -4380408335715216687L;

 private Integer id;
 private String title;
 private List<ProductPicView> imgurl;
 private String publishtime;
 private String titlepic;
 private String content;
 private String beizhu;
 private String status;
 private String renyuanId;
 private String pid;

 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public List<ProductPicView> getImgurl() {
  return imgurl;
 }

 public void setImgurl(List<ProductPicView> imgurl) {
  this.imgurl = imgurl;
 }

 public String getPublishtime() {
  return publishtime;
 }

 public void setPublishtime(String publishtime) {
  this.publishtime = publishtime;
 }

 public String getTitlepic() {
  return titlepic;
 }

 public void setTitlepic(String titlepic) {
  this.titlepic = titlepic;
 }

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public String getBeizhu() {
  return beizhu;
 }

 public void setBeizhu(String beizhu) {
  this.beizhu = beizhu;
 }

 public String getStatus() {
  return status;
 }

 public void setStatus(String status) {
  this.status = status;
 }

 public String getRenyuanId() {
  return renyuanId;
 }

 public void setRenyuanId(String renyuanId) {
  this.renyuanId = renyuanId;
 }

 public String getPid() {
  return pid;
 }

 public void setPid(String pid) {
  this.pid = pid;
 }

 
 @Override
 public int compareTo(ProductInfoView o) {
  ProductInfoView pview = (ProductInfoView)o;
  Integer id1 = pview.getId();
  Integer id2 = this.getId();
  System.out.println(id2>id1);
  return id2>id1 ? -1 :1; (倒序)
 }

}

 

//测试方法

List<ProductInfoView> list = new ArrayList<ProductInfoView>(); 
  ProductInfoView p1  = new ProductInfoView();
  p1.setId(56);
  p1.setContent("56haha~~~~~~");
  list.add(p1);
  ProductInfoView p2  = new ProductInfoView();
  p2.setId(4);
  p2.setContent("4sss~~~~~~"); 
  
  list.add(p2); 
  
  
  
  Collections.sort(list); 

  for(int i=0;i<list.size();i++){
   System.out.println("id = "+list.get(i).getId());
  }

 

 

/**结果**/
id = 56
id = 4

你可能感兴趣的:(list,String,测试,Integer,Class,sun)