下面是我想要排序的數組的示例。排序在javascript
[ { nums: 'http://s3.amazonaws.com/1375304393109.png',
variant: { name: 'original' } },
{ nums: 'http://s3.amazonaws.com/2315456487896.jpg',
variant: { name: 'original' } },
{ nums: 'http://s3.amazonaws.com/1375304393109.png',
variant: { name: 'r256x200', size: '256x200' } },
{ nums: 'http://s3.amazonaws.com/1375304393091.jpg',
variant: { name: 'r256x200', size: '256x200' } },
{ nums: 'http://s3.amazonaws.com/2315456487896.jpg',
variant: { name: 'r512x400', size: '512x400' } },
{ nums: 'http://s3.amazonaws.com/1375304393091.jpg',
variant: { name: 'r512x400', size: '512x400' } } ]
我想根據nums(string)鍵對數組進行排序。我想對數組進行排序的方法很簡單:讓相同的數字羣彼此相鄰,即
[ { nums: 'http://s3.amazonaws.com/1375304393109.png',
variant: { name: 'original' } },
{ nums: 'http://s3.amazonaws.com/1375304393109.png',
variant: { name: 'r256x200', size: '256x200' } },
{ nums: 'http://s3.amazonaws.com/2315456487896.jpg',
variant: { name: 'original' } },
{ nums: 'http://s3.amazonaws.com/2315456487896.jpg',
variant: { name: 'r512x400', size: '512x400' } },
{ nums: 'http://s3.amazonaws.com/1375304393091.jpg',
variant: { name: 'r256x200', size: '256x200' } },
{ nums: 'http://s3.amazonaws.com/1375304393091.jpg',
variant: { name: 'r512x400', size: '512x400' } } ]
它不必是任何順序,只要同一號碼組了。這樣做的最快方法是什麼?
抱歉有關混淆。我想這是原始問題的稍微複雜的版本。任何人有好主意?
您是否閱讀過此堆棧溢出頁面右列中的「相關」鏈接?我懷疑他們中的一個會有所幫助。 –
.sort(function(a,b){return a.nums-b.nums}); – dandavis