簡體   English   中英

在javascript中使用其中一個的值排序2數組

[英]sort 2 array with the values of one of them in javascript

我有兩個數組,比如說priceArray = [1,5,3,7]

userIdArray = [11,52,41,5]

我需要對priceArray進行排序,以便對userIdArray進行排序。 例如輸出應該是:

priceArray = [1,3,5,7] userIdArray = [11,41,52,5]

任何想法怎么做?

我正在NodeJS中編寫我的服務器

取自使用地圖排序並適用於userIdArray:

 // the array to be sorted var priceArray = [1, 5, 3, 7], userIdArray = [11, 52, 41, 5]; // temporary array holds objects with position and sort-value var mapped = priceArray.map(function (el, i) { return { index: i, value: el }; }); // sorting the mapped array containing the reduced values mapped.sort(function (a, b) { return a.value - b.value; }); // container for the resulting order var resultPrice = mapped.map(function (el) { return priceArray[el.index]; }); var resultUser = mapped.map(function (el) { return userIdArray[el.index]; }); document.write('<pre>' + JSON.stringify(resultPrice, 0, 4) + '</pre>'); document.write('<pre>' + JSON.stringify(resultUser, 0, 4) + '</pre>'); 

正如rrowland建議的那樣,使用適當的數據結構,您可以使用:

 var data = [{ userId: 11, price: 1 }, { userId: 52, price: 15 }, { userId: 41, price: 13 }, { userId: 5, price: 17 }]; data.sort(function (a, b) { return a.price - b.price; }); document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>'); 

ES6略短一點

 var priceArray = [1, 5, 3, 7], userIdArray = [11, 52, 41, 5], temp = Array.from(priceArray.keys()).sort((a, b) => priceArray[a] - priceArray[b]); priceArray = temp.map(i => priceArray[i]); userIdArray = temp.map(i => userIdArray[i]); console.log(priceArray); console.log(userIdArray); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

在不了解整個用例的情況下很難規定更好的解決方案。 也就是說,如果您需要按ID排序,那么創建包含用戶對象的單個數組可能更有意義:

var users = [
  { id: 123, price: 25.00 },
  { id: 124, price: 50.00 }
];

users.sort(function(a, b) {
  return a.id - b.id;
});

或者,如果不需要對它們進行排序,您只需按id創建用戶地圖:

var userPrices = {
  123: 25.00,
  124: 50.00
};

基於Rrowland的答案,您可以使用像lodash這樣的庫創建對象數組:

var prices  = [1, 5, 8, 2];
var userIds = [3, 5, 1, 9];

var pairs = _.zipWith(prices, userIds, function(p, u) {
  return { price: p, userId: u };
}); 

這會給你一個像這樣的對象:

[ 
  { price: 1, userId: 3 },
  { price: 5, userId: 5 },
  ... etc
]

然后,為了排序,您可以簡單地使用Javascript排序:

pairs.sort(function(p) { return p.price });

如果你真的需要它作為一個userIds數組,你可以在排序后得到它:

var sortedUserId = pairs.map( function(p) { return p.userId });
// returns [ 3, 9, 5, 8 ];

我已經看到了關於使不可能的狀態變得不可能的好話題 這涵蓋了2個相關但可能不同步的數組,並且更好地使用具有2個屬性的一個對象數組(如上所述)。

然而; 如果你想改變兩個數組並按照你可以執行以下操作的方式對它們進行排序:

 //this will mutate both arrays passed into it // you could return the arrays but then you need to do arr.slice(0).sort(...) instead const sortSame = sortFn => (arrayToSort,arrayToSortSame) => { const sortResults = []; arrayToSort.sort(//will mutate the array (a,b)=>{ const result = sortFn(a,b); sortResults.push(result); return result } ); arrayToSortSame.sort(()=>sortResults.shift()); return undefined; } const priceArray= [1,5,3,7]; const userIdArray=[11, 52, 41, 5]; const numSortSameAscending = sortSame((a,b)=>ab); numSortSameAscending(priceArray,userIdArray); console.log( priceArray,userIdArray ) 

盡管這個答案中的代碼可能看起來更簡單,但這並不是最便宜的方法,因為映射比排序更便宜(更好地映射3次並排序一次然后排序兩次),具體取決於數組的大小和原始數組亂序多少這種排序方式可能非常昂貴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM