我想对一个json多级进行排序!!首先按伊斯兰教法排序 -> 然后按 Volume[0] -> 然后按市值 [0]
未排序:
[
{
"Name": "Hcl",
"sharia": true,
"Volume": [
25000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "rado",
"sharia": false,
"Volume": [
50000,
"high"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "ssinc ",
"sharia": false,
"Volume": [
200,
"low"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "sureshot",
"sharia": false,
"Volume": [
20000,
"moderate"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "Infosys",
"sharia": true,
"Volume": [
50000,
"high"
],
"marketcap": [
6000,
"large"
]
},
{
"Name": "orchid",
"sharia": true,
"Volume": [
200,
"low"
],
"marketcap": [
6000,
"Large"
]
},
{
"Name": "hero honda",
"sharia": true,
"Volume": [
150,
"low"
],
"marketcap": [
400,
"medium "
]
},
{
"Name": "bioffoz",
"sharia": false,
"Volume": [
40000,
"moderate"
],
"marketcap": [
5000,
"large"
]
}
]
截至目前,我正在使用以下功能进行一次排序:
var sort_by = function(field, reverse, primer){
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
}
}
用于排序 A - Z
company.sort(sort_by('Name', false, function(a){return a.toUpperCase()}));
对于排序布尔值:
company.sort(sort_by('sharia', true, function(a){return Boolean(a)}));
对于从高到低排序:
company.sort(sort_by('sharia', true, parseint));
按伊斯兰教法排序(看起来像这样):
[
{
"Name": "Hcl",
"sharia": true,
"Volume": [
25000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "orchid",
"sharia": true,
"Volume": [
200,
"low"
],
"marketcap": [
6000,
"Large"
]
},
{
"Name": "hero honda",
"sharia": true,
"Volume": [
150,
"low"
],
"marketcap": [
400,
"medium "
]
},
{
"Name": "Infosys",
"sharia": true,
"Volume": [
50000,
"high"
],
"marketcap": [
6000,
"large"
]
},
{
"Name": "ssinc ",
"sharia": false,
"Volume": [
200,
"low"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "sureshot",
"sharia": false,
"Volume": [
20000,
"moderate"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "rado",
"sharia": false,
"Volume": [
50000,
"high"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "bioffoz",
"sharia": false,
"Volume": [
40000,
"moderate"
],
"marketcap": [
5000,
"large"
]
}
]
然后按体积排序
[
{
"Name": "Infosys",
"sharia": true,
"Volume": [
50000,
"high"
],
"marketcap": [
6000,
"large"
]
},
{
"Name": "Hcl",
"sharia": true,
"Volume": [
25000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "orchid",
"sharia": true,
"Volume": [
200,
"low"
],
"marketcap": [
6000,
"Large"
]
},
{
"Name": "hero honda",
"sharia": true,
"Volume": [
150,
"low"
],
"marketcap": [
400,
"medium "
]
},
{
"Name": "rado",
"sharia": false,
"Volume": [
50000,
"high"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "bioffoz",
"sharia": false,
"Volume": [
40000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "sureshot",
"sharia": false,
"Volume": [
20000,
"moderate"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "ssinc ",
"sharia": false,
"Volume": [
200,
"low"
],
"marketcap": [
5000,
"large"
]
}
]
全部排序:
[
{
"Name": "Infosys",
"sharia": true,
"Volume": [
50000,
"high"
],
"marketcap": [
6000,
"large"
]
},
{
"Name": "Hcl",
"sharia": true,
"Volume": [
25000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "orchid",
"sharia": true,
"Volume": [
200,
"low"
],
"marketcap": [
6000,
"Large"
]
},
{
"Name": "hero honda",
"sharia": true,
"Volume": [
150,
"low"
],
"marketcap": [
400,
"medium "
]
},
{
"Name": "rado",
"sharia": false,
"Volume": [
50000,
"high"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "bioffoz",
"sharia": false,
"Volume": [
40000,
"moderate"
],
"marketcap": [
5000,
"large"
]
},
{
"Name": "sureshot",
"sharia": false,
"Volume": [
20000,
"moderate"
],
"marketcap": [
5000,
"small"
]
},
{
"Name": "ssinc ",
"sharia": false,
"Volume": [
200,
"low"
],
"marketcap": [
5000,
"large"
]
}
]