Skip to content

Instantly share code, notes, and snippets.

@unisys12
Created March 13, 2017 12:26
Show Gist options
  • Save unisys12/b0f7a6e1587a7df9d3d7e909c03827a3 to your computer and use it in GitHub Desktop.
Save unisys12/b0f7a6e1587a7df9d3d7e909c03827a3 to your computer and use it in GitHub Desktop.
Sorting Characters Based on Date Last Played
// Edited character summary object returned from Bungie API
var characters = [
{
"characterBase": {
"membershipId": "4611686018431328330",
"membershipType": 1,
"characterId": "2305843009319995052",
"dateLastPlayed": "2017-03-13T01:18:03Z",
"minutesPlayedThisSession": "57",
"minutesPlayedTotal": "19702",
"powerLevel": 397,
"raceHash": 898834093,
"genderHash": 3111576190,
"classHash": 671679327,
"currentActivityHash": 2624916786,
"lastCompletedStoryHash": 0,
"stats": {
// Removed
},
"customization": {
// Removed
},
"grimoireScore": 4765,
"peerView": {
"equipment": [
// Removed
]
},
"genderType": 0,
"classType": 1,
"buildStatGroupHash": 3155986019
},
"levelProgression": {
// Removed
},
"emblemPath": "/common/destiny_content/icons/ca5d81851ae0160d140f0289f9e8fbf9.jpg",
"backgroundPath": "/common/destiny_content/icons/2bb9d04d3c900c4097e6b5d6648b3c98.jpg",
"emblemHash": 185564348,
"characterLevel": 40,
"baseCharacterLevel": 40,
"isPrestigeLevel": false,
"percentToNextLevel": 0
},
{
"characterBase": {
"membershipId": "4611686018431328330",
"membershipType": 1,
"characterId": "2305843009317038347",
"dateLastPlayed": "2017-03-13T00:19:57Z",
"minutesPlayedThisSession": "133",
"minutesPlayedTotal": "21361",
"powerLevel": 399,
"raceHash": 2803282938,
"genderHash": 3111576190,
"classHash": 3655393761,
"currentActivityHash": 3743955707,
"lastCompletedStoryHash": 0,
"stats": {
// Removed
},
"customization": {
// Removed
},
"grimoireScore": 4765,
"peerView": {
"equipment": [
// Removed
]
},
"genderType": 0,
"classType": 0,
"buildStatGroupHash": 3801959103
},
"levelProgression": {
// Removed
},
"emblemPath": "/common/destiny_content/icons/554711da7670d66003fc8064bb1f348c.jpg",
"backgroundPath": "/common/destiny_content/icons/3a429921a7fbe60853fdbea6c1044780.jpg",
"emblemHash": 185564345,
"characterLevel": 40,
"baseCharacterLevel": 40,
"isPrestigeLevel": false,
"percentToNextLevel": 0
},
{
"characterBase": {
"membershipId": "4611686018431328330",
"membershipType": 1,
"characterId": "2305843009370869765",
"dateLastPlayed": "2016-11-20T00:53:06Z",
"minutesPlayedThisSession": "15",
"minutesPlayedTotal": "3375",
"powerLevel": 374,
"raceHash": 2803282938,
"genderHash": 3111576190,
"classHash": 2271682572,
"currentActivityHash": 0,
"lastCompletedStoryHash": 0,
"stats": {
// Removed
},
"customization": {
// Removed
},
"grimoireScore": 4765,
"peerView": {
"equipment": [
// Removed
]
},
"genderType": 0,
"classType": 2,
"buildStatGroupHash": 1997970403
},
"levelProgression": {
// Removed
},
"emblemPath": "/common/destiny_content/icons/0577a447173823c6bfdc1aba73fa0603.jpg",
"backgroundPath": "/common/destiny_content/icons/c652f6ea7bc0ed568d5f23eb82839fbb.jpg",
"emblemHash": 1722936866,
"characterLevel": 40,
"baseCharacterLevel": 40,
"isPrestigeLevel": false,
"percentToNextLevel": 0
}
];
// Create empty array to store properties that I will be sorting against.
var dates = [];
// Use Array.prototype.map to itterate over the array and assign an index
// and the property of "dateLastPlayed" to the dates array
characters.map((prop, i)=>{
dates.push({index: i, value: prop.characterBase.dateLastPlayed})
})
// Sort the dates array, because that is more performant than sorting the
// whole character object.
var result = dates.map(function(el){
return characters[el.index];
});
// Return the results
// did this on JSBin and it works with the above object.
console.log(result)
@unisys12
Copy link
Author

unisys12 commented Mar 15, 2017

The solution ended up being a lot simpler than this by the way...

vm.characterData = response.data.Response.data;

// chars equals the character object in the above sample
let chars = vm.characterData.character

// Empty array to store our active character/s
let active = []

// Find any active characters
// And yes, this could be accomplished with .map or .filter even, but it works, so... 
for (let i = 0; i < chars.length; i++) {
  if (chars[i].characterBase.currentActivityHash > 0) {
    active.push(chars[i]);
  }
}

// Works out that the most recent active character is adding to the first index anyway
// So all I have to do is select it.
vm.activeCharacter = active[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment