JavaScript如何对2D数组进行排序

d4so4syb  于 2023-05-05  发布在  Java
关注(0)|答案(4)|浏览(170)

我有一个2d数组,我想对它进行排序

var result = {
   validationdate : [
      "2023-03",
      "2022-11",
      "2023-01",
      "2023-04"
   ],
   program : [
      "a",
      "a",
      "b",
      "a"
   ],
}
result.validationdate.sort()
return result

但我得到了这个:

{
   "validationdate": [
      "2022-11",
      "2023-01",
      "2023-03",
      "2023-04"
   ],
   "program": [
      "a",
      "a",
      "b",
      "a"
   ]
}

如何像validationdate排序那样对程序列进行排序并获得:

{
   "validationdate": [
      "2022-11",
      "2023-01",
      "2023-03",
      "2023-04"
   ],
   "program": [
      "a",
      "b",
      "a",
      "a"
   ]
}

我注意到validationdate是一个字符串列。

mbzjlibv

mbzjlibv1#

您可以做的一件事是创建一个索引数组,并根据相关的日期对它们进行排序。

// ["2023-03","2022-11","2023-01","2023-04"]
const order = Array.from(result.validationdate.keys());
// [0,1,2,3]
order.sort((a, b) => result.validationdate[a].localeCompare(result.validationdate[b]));
// [1,2,0,3]

使用索引顺序,我们现在可以通过将索引Map到其关联值来替换validationdateprogram的当前值。

result.validationdate = order.map(index => result.validationdate[index]);
result.program = order.map(index => result.program[index]);
var result = {
  validationdate: ["2023-03","2022-11","2023-01","2023-04"],
  program: ["a","a","b","a"],
};

const order = Array.from(result.validationdate.keys());
// [0,1,2,3]
order.sort((a, b) => result.validationdate[a].localeCompare(result.validationdate[b]));
// [1,2,0,3]

result.validationdate = order.map(index => result.validationdate[index]);
result.program = order.map(index => result.program[index]);

console.log(result);
ru9i0ody

ru9i0ody2#

您可以通过这种方式实现:
1.创建一个orderList,其中包含验证日期和程序的键值对
1.然后排序validationdate
1.然后在排序数组上循环并从orderList中获取Map值。分配给result.program

function getSortedValue() {
  var result = {
    validationdate: ["2023-03", "2022-11", "2023-01", "2023-04"],
    program: ["a", "a", "b", "a"],
  }
  const order = result.validationdate.reduce(
    (acc, item, index) => ({...acc, [item]: result.program[index] }),
    {}
  )
  result.validationdate.sort()
  result.program = result.validationdate.map((item) => order[item])
  return result
}

console.log(getSortedValue())
zte4gxcn

zte4gxcn3#

要根据已排序的validationdate数组对program数组进行排序,我们可以创建一个包含两个数组中的值的对象数组。然后我们可以根据validationdate属性对这个数组进行排序,并从排序后的对象数组中提取排序后的程序数组。

const result = {
  validationdate: ["2023-03", "2022-11", "2023-01", "2023-04"],
  program: ["a", "a", "b", "a"],
};

// create an array of objects with corresponding values
let array = result.validationdate.map(function(value, index) {
  return {
    validationdate: value,
    program: result.program[index],
  };
});

// sort the array based on validationdate
array.sort(function(a, b) {
  return new Date(a.validationdate) - new Date(b.validationdate);
});

// extract/split the sorted array
let sortedArrays = {
  program: [],
  validationdate: []
};

array.map(function(value) {
  sortedArrays.program.push(value.program);
  sortedArrays.validationdate.push(value.validationdate);
});

// log
console.log(sortedArrays);
a6b3iqyw

a6b3iqyw4#

let temp1 = result.validationdate.map((item,index)=>({date:item,program: result.program[index]}));
temp1.sort((a,b)=>new Date(a.date)-new Date(b.date))

输出:- [ {“date”:“2022-11”、“方案”:“a”},{“date”:“2023-01”,“程序”:“B”},{“date”:“2023-03”,“程序”:“a”},{“date”:“2023-04”,“程序”:“a”} ]那么只有我们想再次形成我们的对象

相关问题