Friday, April 20, 2007

Javascript 多次元配列のソートの例

<script type=text/javascript>
<!--

function sort1(a,b){ return a[1] - b[1] }
function sort2(a,b){ return a[2] - b[2] }

xx = new Array(3, 7, 8, 1);
xx.sort();
document.write(xx);
document.write("<br>");
var persons =
[

[ 'Tanaka' , '32' ,'Japan'] ,
[ 'John' , '38' ,'UK'] ,
[ 'Ken' , '26' ,'France'] ,
[ 'John' , '49' ,'India'] ,

]
persons.sort();

for (var i = 0; i < persons.length; i ++) {
document.write(persons[i] + "<br>");
}
document.write("<br>");
persons.sort(sort1)

for (var i = 0; i < persons.length; i ++) {
document.write(persons[i] + "<br>");
}

document.write("<br>");
persons.sort(sort2)

for (var i = 0; i < persons.length; i ++) {
document.write(persons[i] + "<br>");
}


//-->
</script>


意外と遅いらしい。

No comments: