2017-04-02 13 views
0

不変のデータを日付順にソートしようとしていますが、なぜこれが機能しないのか分かりません。いくつかの例では、バニラのjavascriptソート関数を使用し、いくつかはキーのget/setメソッドを使用しています。私は運が無ければ両方を試みた。リストの代わりにマップを使うべきですか?日付で不変のデータをソートする方法

const data = [{"dateTimeMoved":1490884969509,"dateTimeUploaded":"2017-03-30 09:42:51.970","latNew":"29.839658296606263","latOld":"29.85996055603027","lngNew":"-95.20913287997246","lngOld":"-95.18970489501953","marketId":"13","subdivisionId":"249044","subdivisionName":"Tidwell Lakes Ranch","territoryId":"4899","territoryName":"Central NE","userId":"1a94157a-bb38-4157-95ea-2ff8b43113e2","userName":"Rudy Sanchez","status":"denied","apprDenyDate":1491067499657,"comment":"Deny-- now approved, now denied","apprDenyById":"1a94157a-bb38-4157-95ea-2ff8b43113e2","apprDenyByName":"Rudy Sanchez"},{"dateTimeMoved":1490884636849,"dateTimeUploaded":"2017-03-30 09:37:17.924","latNew":"29.801395477185157","latOld":"29.822208688618133","lngNew":"-95.20630147308111","lngOld":"-95.20605336874723","marketId":"13","subdivisionId":"246090","subdivisionName":"Hidden Meadow","territoryId":"4899","territoryName":"Central NE","userId":"1a94157a-bb38-4157-95ea-2ff8b43113e2","userName":"Rudy Sanchez","status":"approved","apprDenyDate":1490884662547,"comment":"Approved this move","apprDenyById":"1a94157a-bb38-4157-95ea-2ff8b43113e2","apprDenyByName":"Rudy Sanchez"}] 
 

 
const immutableData = Immutable.List(data) 
 

 

 
const sortMarkerMoveDate = (option, array) => { 
 

 
    switch (option) { 
 
    case 'dateTimeMoved': 
 
     array.sort((a, b) => new Date(a.dateTimeMoved) - new Date(b.dateTimeMoved)) 
 
     break 
 
    case '-dateTimeMoved': 
 
     array.sort((a, b) => new Date(b.dateTimeMoved) - new Date(a.dateTimeMoved)) 
 
     break 
 
    } 
 
    return array 
 
} 
 

 
const dateTimeOption1 = '-dateTimeMoved' 
 
const dateTimeOption2 = 'dateTimeMoved' 
 
const result = sortMarkerMoveDate(dateTimeOption2, immutableData) 
 

 
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>

答えて

1

ここでの問題は、.sortは、それがソートされたリストを変異させるのではなく、新しいソートされたリストを返さないことです。だから

const data = [{"dateTimeMoved":2490884636849}, {"dateTimeMoved":10}, {"dateTimeMoved":1490884969509},{"dateTimeMoved":1490884636849}, {"dateTimeMoved":0}] 

const immutableData = Immutable.List(data) 


const sortMarkerMoveDate = (option, array) => { 
    switch (option) { 
    case 'dateTimeMoved': 
     return array.sort((a, b) => new Date(a.dateTimeMoved) - new Date(b.dateTimeMoved)) 
    case '-dateTimeMoved': 
     return array.sort((a, b) => new Date(b.dateTimeMoved) - new Date(a.dateTimeMoved)) 
    default: 
     return array; 
    } 
} 

const dateTimeOption1 = '-dateTimeMoved' 
const dateTimeOption2 = 'dateTimeMoved' 
const result = sortMarkerMoveDate(dateTimeOption2, immutableData) 

console.log(result.toJS()) 

だけですぐにarray.sort(…)を返す:

これは(テスト用dataより良いを追加しました)あなたのために動作します。

+0

ありがとうございました。 – texas697

関連する問題