Blogger Information
Blog 21
fans 0
comment 0
visits 12364
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js 实现数组排序 获取最大值,最小值,翻转
中天行者
Original
607 people have browsed it
  1. <script>
  2. function MyArray() {
  3. this.mySort=function (array,sort='asc') {
  4. var max = null;
  5. // var array = arguments[0];
  6. // var sort =arguments[1]==='asc'? 'asc' :'desc';
  7. for(var i=0; i<array.length-1;i++ ){
  8. for(var j=0; j<array.length-1;j++){
  9. //升序
  10. if(sort==='asc'){
  11. //当前 数 和 后一个数对比
  12. if(array[j] > array[j+1]){
  13. max=array[j]; //把较大的数赋值给max
  14. array[j]=array[j+1];//较小的数丢到前面
  15. array[j+1]=max;//较大的数丢到后面
  16. }
  17. //降序
  18. }else{
  19. //当前 数 和 后一个数对比
  20. if(array[j] < array[j+1]){
  21. max=array[j+1];
  22. //较小的数丢到后面
  23. array[j+1]=array[j];
  24. //大的数丢到前面
  25. array[j]=max;
  26. }
  27. }
  28. }
  29. }
  30. return arguments[0]=array;
  31. }
  32. this.myReverse=function () {
  33. var newArray= new Array();
  34. for(var i=0;i<=arguments[0].length-1;i++){
  35. newArray[i]=arguments[0][arguments[0].length-(i+1)]
  36. }
  37. return newArray;
  38. }
  39. this.myMax=function(){
  40. var array =arguments[0];
  41. var max= array[0];
  42. for(var i=0;i<array.length;i++){
  43. if(array[i] > max){
  44. max= array[i];
  45. }
  46. }
  47. return max ;
  48. // return this.mySort(arguments[0])[0]
  49. }
  50. this.myMin=function () {
  51. var array =arguments[0];
  52. var min= array[0];
  53. for(var i=0;i<array.length;i++){
  54. if(array[i] < min){
  55. min= array[i];
  56. }
  57. }
  58. return min ;
  59. }
  60. }
  61. var arr = new MyArray();
  62. var a= [1,90,5,4,10,9,8];
  63. console.log(arr.mySort(a,'asc'));
  64. console.log(arr.myReverse(a));
  65. console.log(arr.mySort(a,'asc'));//升序
  66. console.log(arr.mySort(a,'desc'));//降序
  67. console.log(arr.myMax(a));
  68. console.log(arr.myMin(a));
  69. </script>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post