`

java Collections排序

    博客分类:
  • java
 
阅读更多

public Map<String, Object> find(DBObject query, Integer page, Integer rows) throws ParseException {
  DBCollection col = MongoUtil.getCollection(collection);

  if (page > 0) {
   page = page - 1;
  } else {
   page = 0;
  }

  Map<String, Object> result = new HashMap<String, Object>();

  Integer total = col.find(query).count();
  List<DBObject> list = new ArrayList<DBObject>();

  if (total > 0) {
   DBObject orderBy = new BasicDBObject();
   orderBy.put("starttime", -1);// 降序
   DBCursor cursor = col.find(query).sort(orderBy).limit(rows).skip(page * rows);

   while (cursor.hasNext()) {
    DBObject object = cursor.next();
    object.put("liveid", object.get("_id").toString());
    object.removeField("_id");
    object = setLiveStatus(object);
    list.add(object);
   }
  }
  Collections.sort(list, new Comparator<DBObject>() {
  
  public int compare(DBObject arg0, DBObject arg1) {
      int result = Integer.valueOf(String.valueOf(arg0.get("statusStr"))) - Integer.valueOf(String.valueOf(arg1.get("statusStr")));
    
   return result;
  
  }
  
  
  });
  result.put("totalrows", total);
  result.put("totalpage", MongoUtil.getTotalPage(rows, total));
  result.put("page", page + 1);
  result.put("rows", rows);
  result.put("list", list);

  return result;
 }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics