回答編集履歴

2 修正

Zuishin

Zuishin score 24388

2017/03/10 22:09  投稿

こんな感じでどうですか?
```JavaScript
var arr =[
{
  "no": 1,
  "name": "りんご",
  "score_1": 76,
  "score_2": 33,
  "score_3": 46
},
{
  "no": 2,
  "name": "ばなな",
  "score_1": 100,
  "score_2": 31,
  "score_3": 56
},
{
  "no": 3,
  "name": "みかん",
  "score_1": 78,
  "score_2": 90,
  "score_3": 89
},
{
  "no": 4,
  "name": "いちじく",
  "score_1": 83,
  "score_2": 32,
  "score_3": 87
}
];
sort_flag = 0;
var sort_flag = 0;
sort_flag = sort_any("score_2", sort_flag);
function sort_any(field, flag) {
   f = flag * 2 - 1;
   var f = flag * 2 - 1;
   arr.sort(function(a, b) {
       return (a[field] - b[field]) * f;
   });
   return 1 - flag;
}
```
1 修正

Zuishin

Zuishin score 24388

2017/03/10 22:07  投稿

こんな感じでどうですか?
```JavaScript
var arr =[
{
  "no": 1,
  "name": "りんご",
  "score_1": 76,
  "score_2": 33,
  "score_1": 46
  "score_3": 46
},
{
  "no": 2,
  "name": "ばなな",
  "score_1": 100,
  "score_2": 31,
  "score_1": 56
  "score_3": 56
},
{
  "no": 3,
  "name": "みかん",
  "score_1": 78,
  "score_2": 90,
  "score_3": 89
},
{
  "no": 4,
  "name": "いちじく",
  "score_1": 83,
  "score_2": 32,
  "score_3": 87
}
];
sort_flag = 0;
sort_flag = sort_any("score_2", sort_flag);
function sort_any(field, flag) {
   f = flag * 2 - 1;
   arr.sort(function(a, b) {
       return (a[field] - b[field]) * f;
   });
   return 1 - flag;
}
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る