2017-03-05 14 views
1

Javascriptを使用してJSON項目を別の配列でソートし、残りの項目をアルファベット順でソートしたいと思います。Javascriptを別の配列で、残りをアルファベット順にソート

var order = [3,9,50,7]; 

そして、私は順序配列を使用してソートする「ID」キーでJSON、との残りの部分:

私は、JSONのアイテムを取得したいための配列を持っています一致しない項目は「名前」キーを使用してください。

ここでは、元のJSONです:

var data = [ 
    { 
     "id": "9", 
     "title": "B" 
    }, 
    { 
     "id": "63", 
     "title": "Z" 
    }, 
    { 
     "id": "433", 
     "title": "D" 
    }, 
    { 
     "id": "50", 
     "title": "A" 
    }, 
    { 
     "id": "2", 
     "title": "G" 
    } 
] 

そして、これは私がそれのようになりたい最終的な結果である:

var data = [ 
    { 
     "id": "9", 
     "title": "B" 
    }, 
    { 
     "id": "50", 
     "title": "A" 
    }, 
    { 
     "id": "433", 
     "title": "D" 
    }, 
    { 
     "id": "2", 
     "title": "G" 
    }, 
    { 
     "id": "63", 
     "title": "Z" 
    } 
] 

答えて

0

これを試してみてください:

var order = [3,9,50,7]; 
var data = [ 
    { 
     "id": "9", 
     "title": "B" 
    }, 
    { 
     "id": "63", 
     "title": "Z" 
    }, 
    { 
     "id": "433", 
     "title": "D" 
    }, 
    { 
     "id": "50", 
     "title": "A" 
    }, 
    { 
     "id": "2", 
     "title": "G" 
    } 
] 
    let sortedArray = data.sort((a, b) => { 
      return ((order.indexOf(a.id) || Number.Max_VALUE) - (order.indexOf(b.id) || Number.MAX_VALUE) || a.title.localeCompare(b.title)) 
     }) 
     console.log(sortedArray) 
+0

リストの先頭に 'order'でIDを持つこの種のアイテムをどうしますか? –

+1

@ダノそうです。私の答えを更新しました。 – nitte93user3232918

2

あなたが使用することができますソート順のオブジェクトと、他のidを最後まで移動するデフォルト値です。

var order = [3, 9, 50, 7], 
 
    data = [{ id: "9", title: "B" }, { id: "63", title: "Z" }, { id: "433", title: "D" }, { id: "50", title: "A" }, { id: "2", title: "G" }], 
 
    orderO = {}; 
 

 
order.forEach(function (a, i, aa) { 
 
    orderO[a] = i - aa.length; // use negative values and zero as default 
 
}); 
 

 
data.sort(function (a, b) { 
 
    return (orderO[a.id] || 0) - (orderO[b.id] || 0) || a.title.localeCompare(b.title); 
 
}); 
 

 
console.log(data); 
 
console.log(orderO);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

ありがとう、ニーナ、良い作品に見えます! :)私はまた、ここで提供されている他の回答をチェックして、どれが最も効率的かを確認します。 –

1

まず、あなたのorder配列と一致し、あなたのdata配列内の項目を見つけます。その後、カスタムソートアルゴリズムを使用して残りの項目をソートし、結果を連結します。

var order = [3, 9, 50, 7]; 
 
var data = [{ "id": "9", "title": "B" }, { "id": "63", "title": "Z" }, { "id": "433", "title": "D" }, { "id": "50", "title": "A" }, { "id": "2", "title": "G" }]; 
 

 
function sortArr(arr, order) { 
 
    var newArr = []; 
 
    for (var i = 0; i < order.length; i++) { 
 
     var index = arr.findIndex(x => x.id === order[i].toString()); 
 
     if (index >= 0) { 
 
      newArr.push(arr.splice(index, 1)); 
 
     } 
 
    } 
 

 
    return newArr.concat(arr.sort((a, b) => a.title > b.title ? 1 : -1)); 
 
} 
 
console.log(sortArr(data, order));

1

あなたは最初のキー要素と値が配列にインデックスされている一つのオブジェクトにご注文の配列を減らすことができます。そしてsortを使って、配列内にある要素で最初にソートし、配列内の位置によってソートし、最後に他の要素をタイトルでソートします。

var data = [{"id":"9","title":"B"},{"id":"63","title":"Z"},{"id":"433","title":"D"},{"id":"50","title":"A"},{"id":"2","title":"G"}] 
 

 
var order = [3,9,50,7].reduce(function(r, e, i) { 
 
return r[e] = i, r 
 
}, {}) 
 

 
var result = data.sort(function(a, b) { 
 
return ((order[b.id] != undefined) - (order[a.id] != undefined) || (order[a.id] - order[b.id])) || 
 
    \t a.title.localeCompare(b.title) 
 
}) 
 

 
console.log(result)

0

1つのソート機能で行うことができます。 Array.indexOfを使用して、順序配列のIDをソートする値を取得します。 1つのアイテムのみが配列の配列にある場合、常に上に移動します。他のすべてのケースはアルファベット順にソートされます。

var order = [3,9,50,7]; 
 

 
var data = [ 
 
    {id: "9", title: "B"}, 
 
    {id: "63", title: "Z"}, 
 
    {id: "433", title: "D"}, 
 
    {id: "50", title: "A"}, 
 
    {id: "2",title: "G"}, 
 
    {id: "9",title: "F"}, 
 
    {id: "7",title: "D"}, 
 
    {id: "3",title: "E"}, 
 
    {id: "2",title: "F"}, 
 
] 
 
// the sort function 
 
function sortFunc(a,b){ 
 
    var iA = order.indexOf(Number(a.id))+1; // get index of A 
 
    var iB = order.indexOf(Number(b.id))+1; // get index of B 
 
    if(iA && iB){ // if both in the order array 
 
     return iA === iB ? // if the same in order array then sort alphabet 
 
      a.title.localeCompare(b.title) : 
 
      iA - iB; 
 
    } 
 
    // if A or B in order array move up else sort alphabet 
 
    return iA ? -1 : iB ? 1 : a.title.localeCompare(b.title); 
 
} 
 
// sort data 
 
data.sort(sortFunc); 
 
// dispay result 
 
data.forEach(obj=>console.log(obj.id + ":" + obj.title))

関連する問題