这是某个大型企业的面试题扩展得来的,只是一种解法,但是无法确定是否高效,不过效果确实是实现了!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" > function concatAndSortArray(array1, array2) { if (arguments.length < 2) { return; } var arg = null; var array = []; for (var i = 0; i < arguments.length; i++) { arg = arguments[i]; if (typeof arg === "object" && arg.length > 0) { array = array.concat(arg); } } array.sort(function (arg1, arg2) { if (arg1 > arg2) { return 1; } else { return -1; } }); return array; } var array1 = [1, 2, 3, 4, 5, 15]; var array2 = [10, 12, 13, 25, 35]; var array = concatAndSortArray(array1, array2); console.log(array); </script> <style type="text/css" > #tx { line-height:30px; font-weight:bolder; text-align:center; background:#DDDDDD; margin-top:10px;} </style> </head> <body> <div>haha</div> <hr /> <div> </div> </body> </html>
更多关于JavaScript数组及字符串相关内容感兴趣的读者可查看本站专题:《JavaScript数组操作技巧总结》及《javascript 字符串相关操作汇总》