我有json对象,想要根据variablevalue对整个json对象进行排序。
的JSONObject:
{
"Surveyors, Cartographers, and Photogrammetrists":[
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":53401.018
},
"variablename":"Average
annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000.0
},
"variablename":"Median annual wage
($)"
},
{
"variablevalue":{
"color":"Blue",
"y":93.7004
},
"variablename":"Offshorability rank (0-100)"
},
{
"variablevalue":{
"color":"Black",
"y":77.76633
},
"variablename":"Male (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":70.81703333333334
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":55.7
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"Blue",
"y":34.8
},
"variablename":"Labor Shortage Index (Flexibility)"
},
{
"variablevalue":{
"color":"Black",
"y":23.87058222069003
},
"variablename":"Projected Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":23.456978920690027
},
"variablename":"Projected New Entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":18.2
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"Blue",
"y":15.606653247064578
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (%
of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":14.623767403
},
"variablename":"Average years
of schooling (years)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":14.0
},
"variablename":"Typical years
of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":12.93433
},
"variablename":"Immigrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":12.622308824363992
},
"variablename":"Projected
Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":11.47879
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":10.80817
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkRed",
"y":8.5
},
"variablename":"Labor Shortage Index
(Demand-Supply)"
},
{
"variablevalue":{
"color":"Blue",
"y":5.29082
},
"variablename":"Teleworkers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":2.0697345889048466
},
"variablename":"Unionized workers
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":1.98172
},
"variablename":"Part-time
workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":1.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.4136033
},
"variablename":"Projected
Re-entrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":0.0
},
"variablename":"Typical
work experience (0-2)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.0
},
"variablename":"Typical training
(0-6)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":-2.984344422700591
},
"variablename":"Projected Employment
Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":-11.248273396326036
},
"variablename":"Projected
Demand-Supply gap (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":-16.71060203279864
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":443900.0
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":443000.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":56048.869
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000 .0
},
"variablename":"Median annual wage ($)"
},
{
"variablevalue":{
"color":"Blue",
"y":80.75036
},
"variablename":"Male (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":70.81703333333334
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":63.3
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":59.199999999999996
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"Blue",
"y":57.199999999999996
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"Black",
"y":55.2
},
"variablename":"Labor
Shortage Index (Flexibility)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":54.9048
},
"variablename":"Offshorability
rank (0-100)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":26.00451467268623
},
"variablename":"Projected
Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":25.80135440180587
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":18.895277715376846
},
"variablename":"Projected Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":18.481674415376844
},
"variablename":"Projected New Entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":15.282309999999999
},
"variablename":"Immigrants (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":14.464026496923166
},
"variablename":"Unionized workers
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":14.094928248999999
},
"variablename":"Average
years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":14.0
},
"variablename":"Typical
years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":11.00492
},
"variablename":"Skill
measure*"
},
{
"variablevalue":{
"color":"Blue",
"y":7.58775
},
"variablename":"Part-time workers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":7.109236957309385
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":3.35128
},
"variablename":"Part-time
workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":2.049
},
"variablename":"Teleworkers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":1.0
},
"variablename":"Certification
measure (0-2)"
},
{
"variablevalue":{
"color":"Black",
"y":0.4136033
},
"variablename":"Projected Re-entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.2031602708803648
},
"variablename":"Projected Employment Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":0.0
},
"variablename":"Typical work experience (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.0
},
"variablename":"Typical
training (0-6)"
},
{
"variablevalue":{
"color":"Black",
"y":-1.9212370833226702
},
"variablename":"Projected
Demand-Supply gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":57299.999237
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":52900.001526
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":50277.343
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":45000.0
},
"variablename":"Median annual
wage ($)"
},
{
"variablevalue":{
"color":"Blue",
"y":96.001
},
"variablename":"Probability of computerisation
(%)"
},
{
"variablevalue":{
"color":"Black",
"y":91.34864999999999
},
"variablename":"Male (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":61.4
},
"variablename":"Labor Shortage Index (Flexibility
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50.0
},
"variablename":"Labor Shortage Index (Education & Skills
)"
},
{
"variablevalue":{
"color":"Blue",
"y":20.375618388959438
},
"variablename":"Projected Labor Supply
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":20.3
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":19.962015088959436
},
"variablename":"Projected New Entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":19.02268386761445
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":13.730615929999999
},
"variablename":"Average years of schooling (years)"
},
{
"variablevalue":{
"color":"Black",
"y":12.5
},
"variablename":"Offshorability rank (0-100)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":12.0
},
"variablename":"Typical years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":11.343804688574572
},
"variablename":"Projected Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":11.27454
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":10.76173
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":10.299999999999999
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":5.6590300000000004
},
"variablename":"Immigrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":4.123637228124855
},
"variablename":"Unionized workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":3.96216
},
"variablename":"Teleworkers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":3.0
},
"variablename":"Typical training (0-6)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":2.1993300000000002
},
"variablename":"Part-time workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.4136033
},
"variablename":"Projected Re-entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Black",
"y":0.0
},
"variablename":"Typical work experience (0-2)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":-7.6788791790398765
},
"variablename":"Projected Employment Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":-9.031813700384866
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Black",
"y":-15.689753054049923
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":59299.999237
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":58259.97
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":56599.998474
},
"variablename":"Employment
, 2014 (persons)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000.0
},
"variablename":"Median annual wage
($)"
},
{
"variablevalue":{
"color":"Blue",
"y":90.10000000000001
},
"variablename":"Labor Shortage Index
(Flexibility)"
},
{
"variablevalue":{
"color":"Black",
"y":82.74463
},
"variablename":"Male (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":79.3
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"DarkRed",
"y":72.8
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"Blue",
"y":59.8
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"Black",
"y":37.45583274660072
},
"variablename":"Projected Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":32.685513248729556
},
"variablename":"Projected Replacement of Jobs, 2014-2024
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":27.859418869633
},
"variablename":"Projected
Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":26.516515869633
},
"variablename":"Projected New Entrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":19.9168
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":16.048081623
},
"variablename":"Average years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":16.0
},
"variablename":"Typical
years of schooling (years)"
},
{
"variablevalue":{
"color":"Blue",
"y":12.5
},
"variablename":"Offshorability
rank (0-100)"
},
{
"variablevalue":{
"color":"Black",
"y":10.97879
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":9.59849
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":9.59641387696772
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Blue",
"y":6.93850826190742
},
"variablename":"Unionized workers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":5.74764
},
"variablename":"Teleworkers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":5.03074
},
"variablename":"Immigrants (% of 2014
employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":4.770319497871167
},
"variablename":"Projected Employment
Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":1.342903
},
"variablename":"Projected Re-entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":1.30934
},
"variablename":"Part-time workers
younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":1.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":0.782686
},
"variablename":"Typical
work experience (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.0
},
"variablename":"Typical training
(0-6)"
},
{
"variablevalue":{
"color":"Black",
"y":-1.8435157600876302
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment, adjusted)"
}
]
}
我尝试过功能
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
});
}
但它仅返回内部json对象并错过主节点文本,如:
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
}
所以排序后的最终结果如下:
"Surveyors, Cartographers, and Photogrammetrists":[
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
}
}
请帮帮我。
答案 0 :(得分:0)
假设您希望按variablevalue.y
值按降序排序,而且永远不会null
或undefined
。
试
obj["Surveyors, Cartographers, and Photogrammetrists"].sort(function(a,b){
return parseFloat(b.variablevalue.y) - parseFloat(a.variablevalue.y);
});
如果variablevalue.y
可以是null
,undefined
或NaN
那么
obj["Surveyors, Cartographers, and Photogrammetrists"].sort(function(a,b){
var by = !isNaN(parseFloat(b.variablevalue.y)) || 0;
var ay = !isNaN(parseFloat(a.variablevalue.y)) || 0;
return by - ay;
});