zhangxinxu / quiz

小测答题收集区
536 stars 43 forks source link

JS基础测试49期 #80

Open zhangxinxu opened 4 years ago

zhangxinxu commented 4 years ago

已知数组:

let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];

其中的数组项均是一个数字带一个字符串,每一个数字和后面的字符串其实是一组。

请实现,整个数组基于数字进行排序(后面的字符自动跟在后面),也就是实现如下所示排序结果:

[1, 'a', 3, 'hi~', 66, 'anyString', 123, '巴啦啦']

大家提交回答的时候,注意缩进距离,起始位置从左边缘开始;另外,github自带代码高亮,所以请使用下面示意的格式。

```js
// 你的JS代码写在这里
 ```

本期小测会以要点形式进行回复。

LuckyRabbitFeet commented 4 years ago

木有思路啊,土办法冒泡走一波。

let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString']
for (let i = 0; i < arr.length - 1; i += 2) {
    if (arr[i] > arr[i + 2]) {
        let temp = arr[i]
        arr[i] = arr[i + 2]
        arr[i + 2] = temp
        temp = arr[i+1]
        arr[i+1] = arr[i + 3]
        arr[i + 3] = temp
    }
}
zer0fire commented 4 years ago

利用 Object.fromEntriesArray.flat 的方式,缺点是把 Number 变成 String

let ary = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString']
let entries= []
for(let i = 0; i < ary.length; i+=2) {
    let chunk = [ary[i],ary[i+1]]
    entries.push(chunk)
};
let temp = Object.fromEntries(entries)
let result = Object.entries(temp).flat()
console.log(result)
// ["1", "a", "3", "hi~", "66", "anyString", "123", "巴啦啦"]

利用 Array.sortArray.flat 的方式

let result = []
for(let i = 0; i < ary.length; i += 2) {
    let chunk = [ary[i], ary[i+1]]
    result.push(chunk)
}
result.sort((a, b) => {
    return  a[0] - b[0]
})
result.flat()
// (8) [1, "a", 3, "hi~", 66, "anyString", 123, "巴啦啦"]
y2x33 commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];
const obj = {};
for(let i = 0, len = arr.length; i < len; i +=2 ){
  obj[arr[i]] = arr[i+1]
}
const reuslt = Object.keys(obj).sort((a,b)=>a-b).flatMap(key=>[Number(key), obj[key]])
console.log(reuslt);
guqianfeng commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];

let obj = {}
arr.forEach((item,index) => {
    if(typeof item === "number"){
        obj[item]=arr[index+1]
    }
})

let resultArr = Object.entries(obj).flat();
console.log(resultArr)
liyongleihf2006 commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString', 1, 'b', 3, '333'];
const hash = {}
for (let i = 0, len = arr.length; i < len; i += 2) {
  let key = arr[i]
  if (hash[key]) {
    hash[key].push(arr[i], arr[i + 1])
  } else {
    hash[key] = [arr[i], arr[i + 1]]
  }
}
Object.keys(hash).sort(function (a, b) {
  return a - b
})
const arr2 = Object.values(hash).flat()
console.log(arr2)
ziven27 commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];
function sortByNum(arr) {
    let arrGroup = [];
    for (let i = 0, len = arr.length; i < len; i++) {
        (i % 2 !== 0) && arrGroup.push([arr[i - 1], arr[i]]);
    }
    arrGroup.sort(([a], [b]) => (a - b));
    return arrGroup.flat();
};
console.log(sortByNum(arr));
livetune commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 1, 'hi~', 66, 'anyString'];
function sort(arr) {
  for (let i = 0; i < arr.length; i += 2) {
    for (let j = i + 2; j < arr.length; j += 2) {
      if (arr[j] < arr[i]) {
        [arr[i], arr[j]] = [arr[j], arr[i]];
        [arr[i + 1], arr[j + 1]] = [arr[j + 1], arr[i + 1]];
      }
    }
  }
}
sort(arr)
console.log(arr)
xxf1996 commented 4 years ago

中规中矩的冒泡排序;

let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString']

function doubleSort (list) {
  let res = list.slice(0)
  if (res.length % 2 === 0) {
    const len = res.length / 2
    for (let i = 0; i < len - 1; i++) { // 冒泡排序
      for (let k = i; k < len; k++) {
        if (res[i * 2] > res[k * 2]) {
          res[i * 2] = [res[k * 2], res[k * 2] = res[i * 2]][0]
          res[i * 2 + 1] = [res[k * 2 + 1], res[k * 2 + 1] = res[i * 2 + 1]][0]
        }
      }
    }
  }

  return res
}

console.log(doubleSort(arr)) // [ 1, 'a', 3, 'hi~', 66, 'anyString', 123, '巴啦啦' ]
zy017 commented 4 years ago
function sortArray(arr) {
  let newArr = []
  arr.forEach((i, k) => {
    if (k % 2 === 0) {
      newArr.push(arr.slice(k, k + 2))
    }
  })
  newArr = newArr.sort((a, b) => a[0] - b[0])
  return newArr.flat()
}
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString']
console.log(sortArray(arr))
zhuishao commented 4 years ago
 let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];
    function sortArr(arr) {
        let arrNum = arr.filter(item => Object.prototype.toString.call(item).includes('Number')).sort((val1, val2) => val1-val2);
        const res = [];
        arrNum.forEach(item => {
            const index = arr.findIndex(x => x === item);
            res.push(arr[index],arr[index+1]);
        })
        return res;
    }
    console.log(sortArr(arr));
Seasonley commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];
let res = arr.flatMap((v,i)=>i%2?[]:[[v,arr[i+1]]])
             .sort((a,b)=>a[0]-b[0]).flat();
console.log(res);
wingmeng commented 4 years ago
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];

方法1:

const mapping = arr.reduce((t, v, i) =>
  (typeof v === 'number' && t.set(v, arr[i + 1]), t), new Map()
);
const result = [...mapping.keys()].sort((a, b) => a - b).reduce((t, v) =>
  [...t, v, mapping.get(v)], []
);
console.log(result);

方法2:

const result = arr
  .reduce((t, v, i) => typeof v === 'number' ? [...t, [v, arr[i + 1]]] : t, [])
  .sort((a, b) => a[0] - b[0])
  .flat();
console.log(result);