一、HTML源碼,可直接運行測試 javascript
<html > <head> <title>jquery排序測試</title> <script type="text/javascript" src="jquery.js"></script> </head> <script language="javascript"> $(function(){ var array = [ {name: 'a', phone: 1}, {name: 'e', phone: 5}, {name: 'c', phone: 3}, {name: 'd', phone: 4}, {name: 'b', phone: 2} ] //默認將序 array.sort(getSortFun('desc', 'phone')); function getSortFun(order, sortBy) { var ordAlpah = (order == 'asc') ? '>' : '<'; var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1'); return sortFun; } alert(JSON.stringify(array)); }); </script> <body> </body> </html>
二、執行結果html