当前位置:Gxlcms > PHP教程 > JavaScript做了个实验的,学习sort函数,有错,我也不会改,求改!

JavaScript做了个实验的,学习sort函数,有错,我也不会改,求改!

时间:2021-07-01 10:21:17 帮助过:23人阅读

JavaScript 做了个实验的,学习sort 函数,有错,我也不会改,求改!

  1. <code>var arr3 =new Array("xxxx510","xxxx12","xxxx16","xxxx18","xxxx13","xxxx14","xxxx15","xxxx8","xxxx7","xxxx6","xxxx1","xxxx5",2 );
  2. arr3.sort(Nat123);
  3. console.log(arr3.toString());
  4. function Nat123(a,b) //自然数升序
  5. {
  6. var aa=a.replace( "xxxx" , "" );
  7. var bb=b.replace( "xxxx" , "" );
  8. if(aa==bb)
  9. {
  10. return 0;
  11. }
  12. else if(aa>bb)
  13. {
  14. return 1; //1 就是第2个在前;
  15. }
  16. else
  17. {
  18. return -1; //-1 就是第1个在前;
  19. }
  20. }
  21. </code>

回复内容:

JavaScript 做了个实验的,学习sort 函数,有错,我也不会改,求改!

  1. <code>var arr3 =new Array("xxxx510","xxxx12","xxxx16","xxxx18","xxxx13","xxxx14","xxxx15","xxxx8","xxxx7","xxxx6","xxxx1","xxxx5",2 );
  2. arr3.sort(Nat123);
  3. console.log(arr3.toString());
  4. function Nat123(a,b) //自然数升序
  5. {
  6. var aa=a.replace( "xxxx" , "" );
  7. var bb=b.replace( "xxxx" , "" );
  8. if(aa==bb)
  9. {
  10. return 0;
  11. }
  12. else if(aa>bb)
  13. {
  14. return 1; //1 就是第2个在前;
  15. }
  16. else
  17. {
  18. return -1; //-1 就是第1个在前;
  19. }
  20. }
  21. </code>

  1. <code>function Nat123(a,b) //自然数升序
  2. {
  3. var aa = (typeof a === 'string') ? a.replace( "xxxx" , "" ) : a;
  4. var bb = (typeof b === 'string') ? b.replace( "xxxx" , "" ) : b;
  5. if(aa==bb)
  6. {
  7. return 0;
  8. }
  9. else if(aa>bb)
  10. {
  11. return 1; //1 就是第2个在前;
  12. }
  13. else
  14. {
  15. return -1; //-1 就是第1个在前;
  16. }
  17. }
  18. var arr3 =new Array("xxxx510","xxxx12","xxxx16","xxxx18","xxxx13","xxxx14","xxxx15","xxxx8","xxxx7","xxxx6","xxxx1","xxxx5",2 );
  19. arr3.sort(Nat123);
  20. console.log(arr3.toString());</code>

  1. <code>var aa=parseInt(a.replace( "xxxx" , "" ));
  2. var bb=parseInt(b.replace( "xxxx" , "" ));</code>

人气教程排行