JavaScript之数组冒泡排序
1.数组元素的拼接
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组元素的拼接</title> </head> <body> </body> </html> <script type="text/javascript"> //concat:拼接 var arr1 = ["一念愚即般若绝"]; var arr2 = ["一念智即般若生"]; var arr = arr1.concat(arr2); console.log(arr); </script>
2.数组元素转换为字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组转换为字符串</title> </head> <body> </body> </html> <script type="text/javascript"> var arr = ["为道法祖仰云际三台鼎峙","玄之又玄","先天地生溯阁中万古灯传","极本无极"]; //join:数组转换为字符串,默认是逗号,join中可添加区分方式 var arrs = arr.join("-"); console.log(arrs); </script>
3.数组冒泡排序
sort:排序字符串和数字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组排序</title> </head> <body> </body> </html> <script type="text/javascript"> //sort:字符串排序和数字排序 var arr = ["ac","ad","aa"]; arr.sort(); console.log(arr); var arr1 = [1,10,115,16]; arr1.sort(); //比较数字和比较字符串一样,逐个比较 console.log(arr1); </script>
sort的冒泡排序写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数组排序</title> </head> <body> </body> </html> <script type="text/javascript"> //sort的冒泡排序写法 //通过a与b的大小 //等于0 大于0 小于0 //区分是升序排序还是降序排序 var arr2 = [1,12,15,123]; arr2.sort(function (a,b){ return a-b; }); console.log(arr2); </script>