Skip to content

Instantly share code, notes, and snippets.

@jackmakesthings
Created July 29, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackmakesthings/d9f50bfcf2df16d89e29 to your computer and use it in GitHub Desktop.
Save jackmakesthings/d9f50bfcf2df16d89e29 to your computer and use it in GitHub Desktop.
Experiment on sorting social data
var data = {
"posts": [{
"title": "Charlie Wilson",
"body": "As the lead singer of The Gap Band, Charlie Wilson had chart-topping hits — and a drug addiction that left him homeless. But determination got him clean and back into the business.",
"upvotes": 2,
"downvotes": 0,
"flags": 0,
"relationship": "friend",
"comments": 4,
"author_repuation": 81
}, {
"title": "Ice Cream",
"body": "New York City is packed with ice cream — and the East Village is the most packed, with 77 shops per square mile. But where's the nearest cone when you need one? The Ice Cream Radar is here to help. With a click of that button, we'll detect your location, scan the vicinity and find your nearest scoop. (Sorry, our data is for New York City only.) New Yorkers without a smartphone (we know you're out there) can text 9292-COOL-ME with your NYC address or location to get a reply with the nearest cold treat",
"upvotes": 32,
"downvotes": 1,
"flags": 1,
"relationship": "none",
"comments": 13,
"author_repuation": 324
}, {
"title": "Sony Movie",
"body": "Sony apparently didn’t care very much that the script to its new sci-fi comedy movie Pixels is pretty bad. It did, however, go to pains to ensure that Pixels would pass Chinese censorship boards with flying colors. Goodbye integrity, hello authoritarian-sanctioned blockbuster.",
"upvotes": 220,
"downvotes": 43,
"flags": 5,
"relationship": "none",
"comments": 67,
"author_repuation": 225
}, {
"title": "Anonymous",
"body": "Once again, the hacktivist collective Anonymous is threatening a spectacular security breach, this time, against the Canadian government. Yesterday, Anonymous hackers told the National Post they’d stolen sensitive Canadian national security documents. They’re prepared to release said documents if the officer who fatally shot a British Columbian protestor last week is not arrested by Monday at 5 p.m., Pacific time.",
"upvotes": 155,
"downvotes": 4,
"flags": 0,
"relationship": "blocked",
"comments": 28,
"author_repuation": 456
}, {
"title": "Friends",
"body": "Remember why you're friends in the first place. Don't judge them for what they have -- what they wear, the trips they take, the house they live in -- but for who they are, and how they treat you.",
"upvotes": 0,
"downvotes": 1,
"flags": 1,
"relationship": "none",
"comments": 1,
"author_repuation": 32
}, {
"title": "Medicare",
"body": "Medicare turns fifty next week. It was signed into law July 30, 1965 -- the crowning achievement of Lyndon Johnson's Great Society. It's more popular than ever. Yet Medicare continues to be blamed for America's present and future budget problems.",
"upvotes": 12,
"downvotes": 90,
"flags": 18,
"relationship": "friend",
"comments": 29,
"author_repuation": 96
}, {
"title": "Grandmaster Flash's Car",
"body": "A Chelsea parking garage is in hot water after an attendant allegedly gave Grandmaster Flash's car to the wrong person. The 57-year-old legendary hip-hop DJ took to Instagram and Twitter last night to air his grievances, alleging that he parked his custom white Dodge Charger at the 101 Car Park on West 23rd Street in Chelsea last week while running errands. When he returned to retrieve it, though, it was gone, and the attendant reportedly claimed he'd given the car to someone 'with no ID who looked like me,' according to Flash.",
"upvotes": 99,
"downvotes": 31,
"flags": 1,
"relationship": "friend",
"comments": 9,
"author_repuation": 304
}, {
"title": "Old City",
"body": "For some people 85 and older, one of the fastest-growing age groups in New York City, where to live can be a decision between safety and autonomy, convenience and happiness.",
"upvotes": 64,
"downvotes": 25,
"flags": 0,
"relationship": "none",
"comments": 58,
"author_repuation": 173
}, {
"title": "Sorry",
"body": "After George Hincapie admitted to doping during his cycling career, he and his brother rescued their sportswear business by asking for forgiveness.",
"upvotes": 30,
"downvotes": 26,
"flags": 5,
"relationship": "blocked",
"comments": 21,
"author_repuation": 244
}, {
"title": "Megabus",
"body": "Tickets for the discount bus company Megabus start at $1, plus some pesky fees. Its routes connect dozens of United States cities from Maine to California (along with more in Canada). Many routes run overnight, meaning you can save on lodging, if you’re up for brushing your teeth at highway rest stops.",
"upvotes": 238,
"downvotes": 73,
"flags": 2,
"relationship": "none",
"comments": 512,
"author_repuation": 309
}]
};
console.log(data.posts[0]["title"]);
var scoreArray = [];
for (i = 0; i < data.posts.length; i++) {
var post = data.posts[i];
console.log(calcScore(post));
}
var sorted = data.posts.sort(function (a, b) {
return (calcScore(b) - calcScore(a));
});
console.log(sorted);
function calcVotes(up, down) {
return (up - down) * 0.35;
}
function calcScore(post) {
var score = calcVotes(post["upvotes"], post["downvotes"]);
score += parseInt(post["comments"]) * 0.5;
score += post["author_repuation"] * 0.25;
post["score"] = score;
return score;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment