对象数组排序,利用jquery
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>jQuery数组和字符串--对象数组排序</title>
- <script type="text/javascript" src="../jquery-1.3.2.min.js"></script>
- <script type="text/javascript">
- <!--
- $(
- function()
- {
- var students =[
- {'sid':'ST001','sname':'张三','sage':18},
- {'sid':'ST004','sname':'赵六','sage':23},
- {'sid':'ST002','sname':'李四','sage':42},
- {'sid':'ST003','sname':'王五','sage':35}
- ];
- //表格显示
- $.each(students,
- function(index, value)
- {
- $('#ia').append('<tr><td>' + value.sid +
- '</td><td>' + value.sname +
- '</td><td>' + value.sage + '</td></tr>');
- }
- );
- //按照SID排序
- var sidOrder = students.sort(
- function(a, b)
- {
- if(a.sid < b.sid) return -1;
- if(a.sid > b.sid) return 1;
- return 0;
- }
- );
- $.each(sidOrder,
- function(index, value)
- {
- $('#ib').append('<tr><td>' + value.sid +
- '</td><td>' + value.sname +
- '</td><td>' + value.sage + '</td></tr>')
- }
- );
- //按照SAGE排序
- var sageOrder = students.sort(
- function(a, b)
- {
- return (a.sage - b.sage);
- }
- );
- $.each(sageOrder,
- function(index, value)
- {
- $('#ic').append('<tr><td>' + value.sid +
- '</td><td>' + value.sname +
- '</td><td>' + value.sage + '</td></tr>')
- }
- );
- }
- );
- //-->
- </script>
- </head>
- <body>
- <h5>未排序对象数组:</h5>
- <table id='ia' border="1"></table>
- <h5>按照SID排序对象数组:</h5>
- <table id='ib' border="1"></table>
- <h5>按照SAGE排序对象数组:</h5>
- <table id='ic' border="1"></table>
- </body>
- </html>
相关推荐
LauraRan 2020-09-28
beibeijia 2020-06-06
zhou0ddw 2020-06-04
breakpoints 2020-05-17
stefan0 2020-04-22
稳哥的小灶 2020-01-04
Nicolase 2019-12-19
zhangxiaojiakele 2019-12-16
DuncanDavid 2019-12-15
roseying 2019-12-13
First00 2019-12-10
stefan0 2019-10-22
softwear 2019-11-08
bush 2019-06-12
wdy00 2017-08-16
罗惠东 2017-08-16
wangchunbo空间 2019-05-24