{"id":16718681,"url":"https://github.com/planeshifter/order","last_synced_at":"2025-04-10T08:53:52.011Z","repository":{"id":27052471,"uuid":"30517823","full_name":"Planeshifter/order","owner":"Planeshifter","description":"Returns a permutation which rearranges an input array.","archived":false,"fork":false,"pushed_at":"2016-11-10T19:50:42.000Z","size":12,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T20:43:23.851Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"CoffeeScript","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/Planeshifter.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":"2015-02-09T04:00:57.000Z","updated_at":"2019-08-13T16:02:37.000Z","dependencies_parsed_at":"2022-09-12T15:03:49.241Z","dependency_job_id":null,"html_url":"https://github.com/Planeshifter/order","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Forder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Forder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Forder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Planeshifter%2Forder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Planeshifter","download_url":"https://codeload.github.com/Planeshifter/order/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248190407,"owners_count":21062277,"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-12T21:38:05.304Z","updated_at":"2025-04-10T08:53:51.990Z","avatar_url":"https://github.com/Planeshifter.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version][npm-image]][npm-url]\n[![Build Status][travis-image]][travis-url]\n[![Dependencies][dependencies-image]][dependencies-url]\n\n\n# order\n\nReturns a permutation which rearranges input array.\n\n## Installation\n\nInstall from npm:\n\n```\nnpm install order-permutation\n```\nUse as follows:\n\n``` javascript\nvar order = require( 'order-permutation' );\n```\n\n## order( arr, \\[compareFunction\\] )\n\nThe exported function requires as its first parameter the array for which the indices in specified order should be obtained. The optional parameter `compareFunction` specifies a function defining the sort order. If not supplied, the returned permutation indices of the  array sort its elements in increasing order. To sort more complicated objects than numeric primitives, a custom `compareFunction` has to be supplied. This function compares elements `a` and `b` according to the following rules:\n- If `compareFunction(a, b)` is less than 0, a gets a lower index than b\n- If `compareFunction(a, b)` is greater than 0, b gets a lower index than a.\n\nTherefore, the standard compare function which sorts the elements in ascending order is equivalent to\n``` javascript\nfunction( a, b ) {\n    if (a \u003c b) {\n      return -1;\n    }\n    if (a \u003e b) {\n      return 1;\n    }\n    return 0;  \n};\n```\n\n### Examples\n\n#### Increasing Sequence:\n\nCode:\n``` javascript\norder([1, 2, 3, 4])\n```\n\nOutput:\n``` javascript\n[0, 1, 2, 3]\n```\n\n#### Decreasing Sequence:\n\nCode:\n``` javascript\norder([4, 3, 2, 1])\n```\n\nOutput:\n``` javascript\n[3, 2, 1, 0]\n```\n\n#### Custom Compare Function:\n\nCode:\n``` javascript\nvar arr = [{name:\"Tom\", age: 28}, {name:\"Lisa\",age:23},{name:\"Bill\", age: 65}]\n// order decreasing with age:\norder(arr, function(a,b){\n  return b.age - a.age;\n});\n```\n\nOutput:\n``` javascript\n[2, 0, 1]\n```\n\n### Obtaining the Sorted Array\n\nTo obtain the actual sorted array, one can use the `at()` function of the *lodash* library, like so:\n\n``` javascript\nvar arr = [5, 1, 3, 2];\nvar indices = order(arr);\n_.at(arr, indices)\n```\n\nOutput:\n``` javascript\n[ 1, 2, 3, 5 ]\n```  \n\n## Unit Tests\n\nRun tests via the command `npm test`\n\n---\n## License\n\n[MIT license](http://opensource.org/licenses/MIT). \n\n[npm-image]: https://badge.fury.io/js/order-permutation.svg\n[npm-url]: http://badge.fury.io/js/order-permutation\n\n[travis-image]: https://travis-ci.org/Planeshifter/order.svg\n[travis-url]: https://travis-ci.org/Planeshifter/order\n\n[dependencies-image]: http://img.shields.io/david/Planeshifter/order.svg\n[dependencies-url]: https://david-dm.org/Planeshifter/order\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Forder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplaneshifter%2Forder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplaneshifter%2Forder/lists"}