{"id":15387191,"url":"https://github.com/wildhoney/setorder","last_synced_at":"2026-01-12T06:55:32.758Z","repository":{"id":57231576,"uuid":"87107924","full_name":"Wildhoney/SetOrder","owner":"Wildhoney","description":"Tiny module for sorting by a set order, using a custom sort function for omitting explicits.","archived":false,"fork":false,"pushed_at":"2017-12-06T10:04:56.000Z","size":98,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-31T09:42:07.368Z","etag":null,"topics":["exact-order","explicit-order","order","ordering","set-order","specific-order"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Wildhoney.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}},"created_at":"2017-04-03T18:37:26.000Z","updated_at":"2023-09-13T23:33:28.000Z","dependencies_parsed_at":"2022-09-04T15:04:47.229Z","dependency_job_id":null,"html_url":"https://github.com/Wildhoney/SetOrder","commit_stats":null,"previous_names":["wildhoney/exactorder"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FSetOrder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FSetOrder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FSetOrder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wildhoney%2FSetOrder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Wildhoney","download_url":"https://codeload.github.com/Wildhoney/SetOrder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239942753,"owners_count":19722330,"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":["exact-order","explicit-order","order","ordering","set-order","specific-order"],"created_at":"2024-10-01T14:52:29.785Z","updated_at":"2026-01-12T06:55:32.722Z","avatar_url":"https://github.com/Wildhoney.png","language":"JavaScript","readme":"# Set Order\n\n\u003e Tiny module for sorting by a set order, using a custom sort function for omitting explicits.\u003cbr /\u003e\u003cbr /\u003e\n\u003e `npm i set-order --save`\u003cbr /\u003e\u003cbr /\u003e\n\u003e Useful for when you have an array of dynamic data, but you need to sort by a set order, rather than a natural sort order, such as alphabetically or numerically.\n\n![Travis](http://img.shields.io/travis/Wildhoney/SetOrder.svg?style=flat-square)\n\u0026nbsp;\n![Coveralls](https://img.shields.io/coveralls/Wildhoney/SetOrder.svg?style=flat-square)\n\u0026nbsp;\n![npm](http://img.shields.io/npm/v/set-order.svg?style=flat-square)\n\u0026nbsp;\n![License MIT](https://img.shields.io/badge/license-mit-lightgrey.svg?style=flat-square)\n\n## Getting Started\n\nTake a scenario where you have a list of bedroom counts where if the `count` is **zero** then it's labelled as **Studio** \u0026mdash; those labelled as such should appear at the beginning of the array.\n\nUsing the native `sort` with a simple comparator function would yield an unsatisfactory result.\n\n```javascript\nconst bedrooms = [4, 2, 'Studio', 1, 3];\nbedrooms.sort((a, b) =\u003e a - b);\n\n// [2, 4, 'Studio', 1, 3]\n```\n\nInstead by utilising `set-order` you're able to be explicit about which items should be grouped together in the sorting process, as well as where they reside in the array \u0026mdash; at the beginning or at the end.\n\n```javascript\nimport { exact } from 'set-order';\n\nconst bedrooms = [4, 2, 'Studio', 1, 3];\nbedrooms.sort(exact(['Studio', 1, 2, 3, 4]));\n\n// ['Studio', 1, 2, 3, 4]\n```\n\n### Sort\n\nTaking the previous approach, its downside is immediately obvious in that you're required to specify **all** of the possible bedroom counts. Instead we'll specify **only** where **Studio** should appear since the other values can be numerically sorted using `a - b`.\n\n```javascript\nimport { exact } from 'set-order';\n\nconst bedrooms = [4, 2, 'Studio', 1, 3];\nbedrooms.sort(exact(['Studio'], (a, b) =\u003e a - b));\n\n// ['Studio', 1, 2, 3, 4]\n```\n\nIt's worth noting that if we didn't pass a comparator function that items that weren't mentioned explicitly will **not** be repositioned.\n\n### Position\n\nBuilding on the previous example we'll add an additional item entitled **etc...** which should appear at the end of the array: `['Studio', ..., 'etc...']` which we can easily achieve by using the `position` property that takes two possible values: `head` and `tail` where the default is `head`.\n\n```javascript\nimport { exact, tail } from 'set-order';\n\nconst bedrooms = [4, 'etc...', 2, 'Studio', 1, 3];\nbedrooms.sort(exact([\n    { value: 'Studio' },\n    { value: 'etc...', position: tail }\n], (a, b) =\u003e a - b));\n\n// ['Studio', 1, 2, 3, 4, 'etc...']\n```\n\n### Associative\n\nAlthough the above is *almost* what we want, in real-life scenarios we're likely to be faced with an array of objects, rather than an array of primitives. For this `set-order` uses the fantastic [`object-path`](https://github.com/mariocasciaro/object-path) module which allows you to specify nested keys in the format `a.b.c` which would select `c` from `{ a: { b: { c: '!' }}}`.\n\n```javascript\nimport { exact, tail } from 'set-order';\nimport by              from 'sort-by';\n\nconst bedrooms = [\n    { id: 1, bedrooms: 4 },\n    { id: 2, bedrooms: 'etc...' },\n    { id: 3, bedrooms: 2 },\n    { id: 4, bedrooms: 'Studio' },\n    { id: 5, bedrooms: 1 },\n    { id: 6, bedrooms: 3 }\n];\n\nbedrooms.sort(exact([\n    { property: 'bedrooms', value: 'Studio' },\n    { property: 'bedrooms', value: 'etc...', position: tail }\n], by('bedrooms')));\n\n// [{ id: 4, bedrooms: 'Studio' }, { id: 5, bedrooms: 1' }] etc...\n```\n\n### Shorthand\n\nUsing the [associative](#Associative) approach we're successfully sorting an array of objects on the key `bedrooms`, but being explicit in saying that **Studio** should appear first \u0026mdash; regardless of how many instances of **Studio** there may be in the array \u0026mdash; and **etc...** should appear at the very end.\n\nHowever memorising the `{ value, property, position }` interface may be somewhat difficult, nor is it too elegant. Instead we can be more succinct and chic by using `head` and `tail` as functions which take two parameters each: `value` and `property` where `property` is optional for an array of primitives.\n \n ```javascript\nimport { exact, head, tail } from 'set-order';\n\nconst bedrooms = [4, 'etc...', 2, 'Studio', 1, 3];\nbedrooms.sort(exact([head('Studio'), tail('etc...')], (a, b) =\u003e a - b));\n\n// ['Studio', 1, 2, 3, 4, 'etc...']\n ```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwildhoney%2Fsetorder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwildhoney%2Fsetorder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwildhoney%2Fsetorder/lists"}