码迷,mamicode.com
首页 > 编程语言 > 详细

傻傻的排序

时间:2019-03-15 10:39:26      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:foreach   includes   turn   length   sam   asc   class   ret   rip   

   let arr = [7, 1, 24, 1, 0, 4, 27, 20]

    function toOrder(arr) {
      let newArr = []
      arr.forEach((a, i) => {
        let samll = newArr[0]
        let big = newArr[newArr.length - 1];
        if (samll == undefined) {
          newArr.push(a)
        } else if (samll > a) {
          newArr = [a, ...newArr];
        } else if (big < a) {
          newArr = [...newArr, a];
        } else if (newArr.includes(a)) {
          //去重
        } else {
          for (let index = 0; index < newArr.length - 1; index++) {
            if (a >= newArr[index] && a <= newArr[index + 1]) {
              newArr = (newArr[index] = [newArr[index], a]) && [].concat(...newArr);
              break
            }
          }
        }
      });
      return newArr
    }
    console.log(toOrder(arr))

应该算的次数少一点吧 大概

  

傻傻的排序

标签:foreach   includes   turn   length   sam   asc   class   ret   rip   

原文地址:https://www.cnblogs.com/lieaqi/p/10535356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!