Open zhangxinxu opened 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
}
}
利用 Object.fromEntries
和 Array.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.sort
和 Array.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, "巴啦啦"]
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);
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)
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)
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));
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)
中规中矩的冒泡排序;
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, '巴啦啦' ]
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))
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));
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);
let arr = [1, 'a', 123, '巴啦啦', 3, 'hi~', 66, 'anyString'];
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);
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);
已知数组:
其中的数组项均是一个数字带一个字符串,每一个数字和后面的字符串其实是一组。
请实现,整个数组基于数字进行排序(后面的字符自动跟在后面),也就是实现如下所示排序结果:
大家提交回答的时候,注意缩进距离,起始位置从左边缘开始;另外,github自带代码高亮,所以请使用下面示意的格式。
本期小测会以要点形式进行回复。