无序的JSONArray在页面重新排序

1.JAVA后取得一个LIST,并转换成JSONArray.

JSONArray  ja= new JSONArray();
    for(Iterator<Info> it = list.iterator();it.hasNext();){
     JSONObject jso = new JSONObject();
    Info info = (Info)it.next();
    jso.put("aa",info.getAa());
    jso.put("bb", info.getBb());
    jso.put("cc",info.getCc());
    }
    response.getWriter().write(ja.toString());

  

2.通过AJAX取回JA并排序,这里是降序,要升序的话,去掉reverse()即可。

$.ajax({
   async : false,
   type : "get",
   url : "/action.do?method=abc",
   contentType:"application/x-www-form-urlencoded; charset=utf-8",
   dataType : 'json',
   data:'',
   success : function(ja) {
                if(ja.length >0){

                         dosomething...
                    }else{

                         dosomething...
                }
                //排序

                var obj =ja;
                obj.sort(function(a,b){
                    return a.a - b.a;
                }).reverse();

   }
  });

相关推荐