jsonマルチレベルを並べ替えたい!! 最初にShariaで並べ替え、次にVolume [0]で並べ替え、次にmarketcap[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"
]
}
]