{"id":15690128,"url":"https://github.com/pubkey/array-push-at-sort-position","last_synced_at":"2025-09-01T20:43:47.763Z","repository":{"id":36328619,"uuid":"223489365","full_name":"pubkey/array-push-at-sort-position","owner":"pubkey","description":"Push items to an array at their correct sort-position","archived":false,"fork":false,"pushed_at":"2024-10-23T21:44:57.000Z","size":93,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-24T01:33:06.649Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pubkey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-22T21:28:11.000Z","updated_at":"2024-10-23T21:44:52.000Z","dependencies_parsed_at":"2023-09-24T08:11:17.058Z","dependency_job_id":"f43596a3-f08c-4b7c-a086-02e3de1c048c","html_url":"https://github.com/pubkey/array-push-at-sort-position","commit_stats":{"total_commits":231,"total_committers":5,"mean_commits":46.2,"dds":0.4069264069264069,"last_synced_commit":"847afb1473a52e11ae7d3e85089b34722985e4f1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Farray-push-at-sort-position","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Farray-push-at-sort-position/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Farray-push-at-sort-position/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pubkey%2Farray-push-at-sort-position/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pubkey","download_url":"https://codeload.github.com/pubkey/array-push-at-sort-position/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248966936,"owners_count":21190872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T18:07:42.363Z","updated_at":"2025-04-14T21:45:56.528Z","avatar_url":"https://github.com/pubkey.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# array-push-at-sort-position\n\nPush items to an array at their correct sort-position which is much faster then re-sorting the array.\n\nAdding an item to an array with `push()` and `sort()` has `O(n*log(n))`\nwhile inserting the item at the correct sort-position has `O(n)`.\n\n```bash\n  npm install array-push-at-sort-position --save\n```\n\n```typescript\n\n// instead of pushing and resorting like this:\nconst arrayWithItems = arrayWithItems.slice();\narrayWithItems.push(newItem);\nconst arrayWithNewItems = arrayWithItems.sort(sortComparator);\n\n// you can push the newItem directly into the correct sorting position\nconst insertPosition = pushAtSortPosition(\n  arrayWithItems,\n  newItem,\n  sortComparator,\n  /**\n   * Start lowest index\n   * Use 0 by default. If you use this method to merge sorted arrays, you might\n   * use a  higher value if you know that the newItem will not be positioned before that index.\n   */\n  0\n);\n```\n\n### Important\n\n- Calling `pushAtSortPosition` will not copy the array. It will mutate the input array. Call `array.slice(0)` on the input\nif you do not want the original array to be mutated.\n\n### See also\n\nI tested many implementations and refactored the best ones. Some other modules:\n\n- [binary-search-insert](https://www.npmjs.com/package/binary-search-insert)\n- [sorted-array](https://github.com/aaditmshah/sorted-array/blob/master/sorted-array.js#L11)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Farray-push-at-sort-position","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpubkey%2Farray-push-at-sort-position","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpubkey%2Farray-push-at-sort-position/lists"}