{"action":"created","issue":{"url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/592","repository_url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code","labels_url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/592/labels{/name}","comments_url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/592/comments","events_url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/592/events","html_url":"https://github.com/Chalarangelo/30-seconds-of-code/pull/592","id":294828617,"number":592,"title":"[FEATURE][ADD] Add stableSort function","user":{"login":"simov","id":1694112,"avatar_url":"https://avatars0.githubusercontent.com/u/1694112?v=4","gravatar_id":"","url":"https://api.github.com/users/simov","html_url":"https://github.com/simov","followers_url":"https://api.github.com/users/simov/followers","following_url":"https://api.github.com/users/simov/following{/other_user}","gists_url":"https://api.github.com/users/simov/gists{/gist_id}","starred_url":"https://api.github.com/users/simov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simov/subscriptions","organizations_url":"https://api.github.com/users/simov/orgs","repos_url":"https://api.github.com/users/simov/repos","events_url":"https://api.github.com/users/simov/events{/privacy}","received_events_url":"https://api.github.com/users/simov/received_events","type":"User","site_admin":false},"labels":[{"id":777547240,"url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/labels/on%20hold","name":"on hold","color":"493baf","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2018-02-06T16:40:14Z","updated_at":"2018-02-07T19:16:21Z","closed_at":"2018-02-07T01:08:19Z","author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/pulls/592","html_url":"https://github.com/Chalarangelo/30-seconds-of-code/pull/592","diff_url":"https://github.com/Chalarangelo/30-seconds-of-code/pull/592.diff","patch_url":"https://github.com/Chalarangelo/30-seconds-of-code/pull/592.patch"},"body":"## New Snippet `stableSort`\r\n\r\nMost browser engines support stable sorting via `Array.sort()` but not Chrome/NodeJS.\r\n\r\nMore information:\r\n- [Sort array items and preserve order of same elements\r\n](https://stackoverflow.com/questions/31213696/sort-array-items-and-preserve-order-of-same-elements)\r\n- [Fast stable sorting algorithm implementation in javascript\r\n](https://stackoverflow.com/questions/1427608/fast-stable-sorting-algorithm-implementation-in-javascript)\r\n\r\n## What does your PR belong to?\r\n- [ ] Website\r\n- [x] Snippets\r\n- [ ] General / Things regarding the repository (like CI Integration)\r\n- [ ] Tests\r\n\r\n## Types of changes\r\n- [ ] Bug fix (non-breaking change which fixes an issue)\r\n- [ ] Enhancement (non-breaking improvement of a snippet)\r\n- [x] New feature (non-breaking change which adds functionality)\r\n- [ ] Breaking change (fix or feature that would cause existing functionality to change)\r\n\r\n## Checklist:\r\n<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->\r\n- [x] My code follows the code style of this project.\r\n- [ ] My change requires a change to the documentation.\r\n- [x] I have updated the documentation accordingly.\r\n- [x] I have checked that the changes are working properly\r\n- [x] I have checked that there isn't any PR doing the same\r\n- [x] I have read the **CONTRIBUTING** document.\r\n"},"comment":{"url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/comments/363878801","html_url":"https://github.com/Chalarangelo/30-seconds-of-code/pull/592#issuecomment-363878801","issue_url":"https://api.github.com/repos/Chalarangelo/30-seconds-of-code/issues/592","id":363878801,"user":{"login":"simov","id":1694112,"avatar_url":"https://avatars0.githubusercontent.com/u/1694112?v=4","gravatar_id":"","url":"https://api.github.com/users/simov","html_url":"https://github.com/simov","followers_url":"https://api.github.com/users/simov/followers","following_url":"https://api.github.com/users/simov/following{/other_user}","gists_url":"https://api.github.com/users/simov/gists{/gist_id}","starred_url":"https://api.github.com/users/simov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simov/subscriptions","organizations_url":"https://api.github.com/users/simov/orgs","repos_url":"https://api.github.com/users/simov/repos","events_url":"https://api.github.com/users/simov/events{/privacy}","received_events_url":"https://api.github.com/users/simov/received_events","type":"User","site_admin":false},"created_at":"2018-02-07T19:16:21Z","updated_at":"2018-02-07T19:16:21Z","author_association":"CONTRIBUTOR","body":"Here is [very simple explanation](https://github.com/mziccard/node-timsort/#stability) of how stable sort works.\r\n\r\nAnd the implementation:\r\n\r\n```js\r\n\r\n// https://github.com/mziccard/node-timsort/#stability\r\n\r\nvar t = require('assert')\r\n\r\n// sorted by weight\r\nvar input = [ \r\n  { height: 100, weight: 80 },\r\n  { height: 90, weight: 90 },\r\n  { height: 70, weight: 95 },\r\n  { height: 100, weight: 100 },\r\n  { height: 80, weight: 110 },\r\n  { height: 110, weight: 115 },\r\n  { height: 100, weight: 120 },\r\n  { height: 70, weight: 125 },\r\n  { height: 70, weight: 130 },\r\n  { height: 100, weight: 135 },\r\n  { height: 75, weight: 140 },\r\n  { height: 70, weight: 140 } \r\n]\r\n\r\n// sorted by height (using stableSort)\r\nvar target = [\r\n  { height: 70, weight: 95 },\r\n  { height: 70, weight: 125 },\r\n  { height: 70, weight: 130 },\r\n  { height: 70, weight: 140 },\r\n  { height: 75, weight: 140 },\r\n  { height: 80, weight: 110 },\r\n  { height: 90, weight: 90 },\r\n  { height: 100, weight: 80 },\r\n  { height: 100, weight: 100 },\r\n  { height: 100, weight: 120 },\r\n  { height: 100, weight: 135 },\r\n  { height: 110, weight: 115 }\r\n]\r\n\r\nvar stableSort = (arr, compare) => arr\r\n  .map((item, index) => ({item, index}))\r\n  .sort((a, b) => compare(a.item, b.item) || a.index - b.index)\r\n  .map(({item}) => item)\r\n\r\nvar compare = (a, b) => a.height - b.height\r\n\r\n// passes\r\nt.deepStrictEqual(stableSort(input, compare), target)\r\n// throws\r\nt.deepStrictEqual(input.sort(compare), target)\r\n\r\n// Array.sort() expects something like this\r\nvar unstable = [\r\n  { height: 70, weight: 140},\r\n  { height: 70, weight: 95 },\r\n  { height: 70, weight: 125 },\r\n  { height: 70, weight: 130 },\r\n  { height: 75, weight: 140 },\r\n  { height: 80, weight: 110 },\r\n  { height: 90, weight: 90 },\r\n  { height: 100, weight: 100 },\r\n  { height: 100, weight: 80 },\r\n  { height: 100, weight: 135 },\r\n  { height: 100, weight: 120 },\r\n  { height: 110, weight: 115 }\r\n]\r\n```\r\n\r\nI've uploaded a [gist](https://gist.github.com/simov/aa3b9e534039fa00ac3c1b4905605069) for convenience.\r\n\r\n@skatcat31 is this test enough to convince you that my implementation is correct?"}}
{"actor":{"display_login":"simov"}}