Javascript 如何在保留顺序的同时对数组进行排序
我有一个数组,例如3,4,3,1,5 按升序或降序排序得到1,3,3,4,5或5,4,3,3,1 我需要原始订单,例如3,3,4,1,5 下划线JS具有groupBy方法,但它将数组拆分为多个组Javascript 如何在保留顺序的同时对数组进行排序,javascript,sorting,underscore.js,Javascript,Sorting,Underscore.js,我有一个数组,例如3,4,3,1,5 按升序或降序排序得到1,3,3,4,5或5,4,3,3,1 我需要原始订单,例如3,3,4,1,5 下划线JS具有groupBy方法,但它将数组拆分为多个组 有什么想法吗?如果要对数组进行适当排序,可以获取每个项目的优先级,并使用优先级对象对其进行排序 let array=[3,4,3,1,5], 指数=0, 优先级={}; forEach(n=>priority[n]=priority[n]| |++索引); array.sort((a,b)=>优先级[
有什么想法吗?如果要对数组进行适当排序,可以获取每个项目的优先级,并使用优先级对象对其进行排序
let array=[3,4,3,1,5],
指数=0,
优先级={};
forEach(n=>priority[n]=priority[n]| |++索引);
array.sort((a,b)=>优先级[a]-优先级[b])
console.log(array)
如果要对数组进行适当排序,可以获取每个项目的优先级,并使用priority对象对其进行排序
let array=[3,4,3,1,5],
指数=0,
优先级={};
forEach(n=>priority[n]=priority[n]| |++索引);
array.sort((a,b)=>优先级[a]-优先级[b])
console.log(数组)
您可以执行以下操作
const arr = [3, 4, 3, 1, 5];
const result = arr.sort((a, b) => a === b || arr.indexOf(a) - arr.indexOf(b));
console.log(result);
你能行
const arr = [3, 4, 3, 1, 5];
const result = arr.sort((a, b) => a === b || arr.indexOf(a) - arr.indexOf(b));
console.log(result);
这是我的长篇大论的解决方案,它可以工作,但威尔可以做精炼
var tempQuestions = [];
// For each Question.
_.each(questionData.questions, function (resultCategory, indexCategory) {
// Get other Question for the same Category, and push to new Array.
tempQuestions.push(_.filter(questionData.questions, function (num) {
return num.category === resultCategory.category;
}));
// Remove all Question for the processes Cateogory (these are now stored in the tempQuestions)
questionData.questions = _.filter(questionData.questions, function (item) {
return item.category !== resultCategory.category;
});
});
// The above processing will create some unwanted blank arrays, remove them.
tempQuestions = _.filter(tempQuestions, function (num) {
return num.length !== 0;
});
// Flatten the array structure to match the original.
questionData.questions = _(tempQuestions).chain()
.zip()
.flatten()
.value();
这是我的长篇大论的解决方案,它可以工作,但威尔可以做精炼
var tempQuestions = [];
// For each Question.
_.each(questionData.questions, function (resultCategory, indexCategory) {
// Get other Question for the same Category, and push to new Array.
tempQuestions.push(_.filter(questionData.questions, function (num) {
return num.category === resultCategory.category;
}));
// Remove all Question for the processes Cateogory (these are now stored in the tempQuestions)
questionData.questions = _.filter(questionData.questions, function (item) {
return item.category !== resultCategory.category;
});
});
// The above processing will create some unwanted blank arrays, remove them.
tempQuestions = _.filter(tempQuestions, function (num) {
return num.length !== 0;
});
// Flatten the array structure to match the original.
questionData.questions = _(tempQuestions).chain()
.zip()
.flatten()
.value();
什么限定了一个组?questionData.questions=..chain(questionData.questions).sortBy('category')。each(函数(resultNew,indexNew){resultNew.order=indexNew+1;})。sortBy('order').value();克隆原始阵列!?这听起来像是一个X-Y问题,特别是考虑到代码注释中的额外上下文。你可能不仅仅是在对数字进行排序,是吗?例如,我们可以用groupBy('category')替换sortBy('category')。但是这改变了输出的格式。什么限定了一个组?questionData.questions=..chain(questionData.questions).sortBy('category')。each(函数(resultNew,indexNew){resultNew.order=indexNew+1;})。sortBy('order').value();克隆原始阵列!?这听起来像是一个X-Y问题,特别是考虑到代码注释中的额外上下文。你可能不仅仅是在对数字进行排序,是吗?例如,我们可以用groupBy('category')替换sortBy('category')。但这会更改output.questionData.questions=uzy.chain(questionData.questions).sortBy('category').each(函数(resultNew,indexNew){resultNew.order=indexNew+1;})的格式。sortBy('order').value();我不是在3,4,3,1,5上排序,我是在一个数组的“Category”属性上排序,这个数组有这些值。如何将其应用于上述情况?我猜
questionData.questions.sort((a,b)=>a.category==b.category | | | questionData.questions.indexOf(a)-questionData.questions.indexOf(b))代码>那个并没有改变,根本并没有对它们进行分组。哦,groupBy也会对类别进行排序,我不希望它这样做。我需要它来保持现有的顺序,只需将它们组合在一起即可。questionData.questions=..chain(questionData.questions).sortBy('category')。each(函数(resultNew,indexNew){resultNew.order=indexNew+1;})。sortBy('order').value();我不是在3,4,3,1,5上排序,我是在一个数组的“Category”属性上排序,这个数组有这些值。如何将其应用于上述情况?我猜questionData.questions.sort((a,b)=>a.category==b.category | | | questionData.questions.indexOf(a)-questionData.questions.indexOf(b))代码>那个并没有改变,根本并没有对它们进行分组。哦,groupBy也会对类别进行排序,我不希望它这样做。我需要它来保持现有的秩序,只是把它们组合在一起。