---
title: "Collections.sort list排序"
author: "xinlanzero"
date: "2011-07-23T13:40:43Z"
tags: []
categories: ["编程语言"]
slug: "collections-sort-list"
draft: false
lastmod: "2011-07-23T13:42:10Z"
id: 1131279
url: "https://xinlanzero.iteye.com/blog/1131279"
---

newList是个Value对象的List，orderId是Value对象的一个属性，现在要求根据orderId把List里的记录降序排序。使用 Collections.sort方法可实现此要求，需要自己写类实现Comparator的 compare接口，这里采用的内部类的方式，当然也可使用其他方式。

Collections.sort(newList, new Comparator&lt;PropertyValue&gt;(){  
 public int compare(Value p1, Value p2) {  
     final int count1 = p1.getOrderId();  
     final int count2 = p2.getOrderId();  
     return (count1 &lt; count2 ? 1 : -1);  
    }  
});