JSONARray 정렬

727 단어 자바
package cn.wmsupvi.use;

import java.util.Comparator;

import net.sf.json.JSONObject;

public class KeyWordSort implements Comparator {

	@Override
	public int compare(JSONObject o1, JSONObject o2) {
		Integer f1 = o1.getInt("count");
		Integer f2 = o2.getInt("count");
		if(f1 instanceof Number && f2 instanceof Number){
			return ((Number)f1).intValue() - ((Number)f2).intValue();
		}else{
			return f1.toString().compareTo(f2.toString());
		}
	}


}

호출
public void sort(JSONArray ja,boolean desc){
	Comparator cj=new KeyWordSort();
	Collections.sort(ja);
	if(desc){
		Collections.reverse(ja);
	}
}

좋은 웹페이지 즐겨찾기