---
title: "根据value字段对map进行排序"
author: "holoblog"
date: "2011-11-04T18:06:00Z"
tags: []
slug: "value-map"
draft: false
lastmod: "2011-11-20T20:08:44Z"
id: 1268528
url: "https://holoblog.iteye.com/blog/1268528"
---

一般来说，我们通常都是通过map中的key字段进行排序，步骤如下：

# 一、根据key对map进行排序

TreeMap&lt;String , Integer&gt; map = new TreeMap&lt;String,Integer&gt;(new Comparator&lt;String&gt;(){ //直接添加比较器

public int compare(String o1,String o2){

.....

}

});

但是却没有直接的方法能够进行value排序；

# 二、根据value对map进行排序

### 步骤如下：

**1.提取视图**

**2.变成ArrayList，并添加比较器**

Set&lt;Map.Entry&lt;String,Integer&gt;&gt;set = map.entrySet();

ArrayList&lt;Map.Entry&lt;String,Integer&gt;&gt; list = new ArrayList&lt;Map.Entry&lt;String,Integer&gt;&gt;(set,new Comparator&lt;Map.Entry&lt;String,Integer&gt;(){

public int compare(Map.Entry&lt;String,Integer&gt; o1,Map.Entry&lt;String,Integer&gt; o2){

int value1 = o1.getValue();

int value2 = o2.getValue();

//接着就可以进行比较了

}

}&gt;);