按时间排序 json table
Sort json table by time
。
你好,
我有这个 json 文件:
{
"fbl": {
"id": "fbl",
"user_id": "fbl@xxx.fr",
"nb_error": 0,
"time": "1:27.700"
},
"dev": {
"id": "dev",
"user_id": "dev@xxx.fr",
"nb_error": 0,
"time": "1:12.54"
},
"jul": {
"id": "jul",
"user_id": "jul@xx.fr",
"nb_error": 0,
"time": "0:58.700"
}
}
我想读取这个文件并按照最短时间到最长时间的顺序对其进行排序(不要修改文件,只需检索值排序以使用 botkit 显示它们)。
首先,为了读取这个文件,我使用:
fs.readFileSync('course.json/quiz.json', 'utf8');
这是个不错的选择?
你能帮我对这些值进行排序然后显示它们吗?
谢谢
如果您想更改顺序,只需翻转比较器函数中的值即可
let data = {
"fbl": {
"id": "fbl",
"user_id": "fbl@xxx.fr",
"nb_error": 0,
"time": "1:27.700"
},
"dev": {
"id": "dev",
"user_id": "dev@xxx.fr",
"nb_error": 0,
"time": "1:12.54"
},
"jul": {
"id": "jul",
"user_id": "fbl@xx.fr",
"nb_error": 0,
"time": "0:58.700"
}
}
const formatTime =(time)=> parseFloat(time.replace(":",""))
const compareTime = (a,b)=>{
let a_time = formatTime(a["time"])
let b_time = formatTime(b["time"])
return a_time-b_time
}
const sort = (data) => {
let keys = Object.keys(data)
let dataAsArray = keys.map(e=>data[e])
return dataAsArray.sort(compareTime)
}
console.log(sort(data))
。 你好,
我有这个 json 文件:
{
"fbl": {
"id": "fbl",
"user_id": "fbl@xxx.fr",
"nb_error": 0,
"time": "1:27.700"
},
"dev": {
"id": "dev",
"user_id": "dev@xxx.fr",
"nb_error": 0,
"time": "1:12.54"
},
"jul": {
"id": "jul",
"user_id": "jul@xx.fr",
"nb_error": 0,
"time": "0:58.700"
}
}
我想读取这个文件并按照最短时间到最长时间的顺序对其进行排序(不要修改文件,只需检索值排序以使用 botkit 显示它们)。
首先,为了读取这个文件,我使用:
fs.readFileSync('course.json/quiz.json', 'utf8');
这是个不错的选择?
你能帮我对这些值进行排序然后显示它们吗?
谢谢
如果您想更改顺序,只需翻转比较器函数中的值即可
let data = {
"fbl": {
"id": "fbl",
"user_id": "fbl@xxx.fr",
"nb_error": 0,
"time": "1:27.700"
},
"dev": {
"id": "dev",
"user_id": "dev@xxx.fr",
"nb_error": 0,
"time": "1:12.54"
},
"jul": {
"id": "jul",
"user_id": "fbl@xx.fr",
"nb_error": 0,
"time": "0:58.700"
}
}
const formatTime =(time)=> parseFloat(time.replace(":",""))
const compareTime = (a,b)=>{
let a_time = formatTime(a["time"])
let b_time = formatTime(b["time"])
return a_time-b_time
}
const sort = (data) => {
let keys = Object.keys(data)
let dataAsArray = keys.map(e=>data[e])
return dataAsArray.sort(compareTime)
}
console.log(sort(data))