{"id":15640622,"url":"https://github.com/jonschlinkert/array-sort","last_synced_at":"2025-04-05T01:05:49.018Z","repository":{"id":57184107,"uuid":"39289166","full_name":"jonschlinkert/array-sort","owner":"jonschlinkert","description":"Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.","archived":false,"fork":false,"pushed_at":"2020-04-01T00:05:16.000Z","size":28,"stargazers_count":74,"open_issues_count":7,"forks_count":24,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-17T19:02:35.919Z","etag":null,"topics":["array","compare","javascript","jonschlinkert","nodejs","sort"],"latest_commit_sha":null,"homepage":null,"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/jonschlinkert.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-07-18T06:31:58.000Z","updated_at":"2024-04-18T07:02:18.000Z","dependencies_parsed_at":"2022-09-10T18:22:21.821Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/array-sort","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Farray-sort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Farray-sort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Farray-sort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Farray-sort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/array-sort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247271528,"owners_count":20911587,"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":["array","compare","javascript","jonschlinkert","nodejs","sort"],"created_at":"2024-10-03T11:38:47.419Z","updated_at":"2025-04-05T01:05:48.985Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# array-sort [![NPM version](https://img.shields.io/npm/v/array-sort.svg?style=flat)](https://www.npmjs.com/package/array-sort) [![NPM monthly downloads](https://img.shields.io/npm/dm/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort)  [![NPM total downloads](https://img.shields.io/npm/dt/array-sort.svg?style=flat)](https://npmjs.org/package/array-sort) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/array-sort.svg?style=flat\u0026label=Travis)](https://travis-ci.org/jonschlinkert/array-sort) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/array-sort.svg?style=flat\u0026label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/array-sort)\n\n\u003e Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save array-sort\n```\n\nInstall with [yarn](https://yarnpkg.com):\n\n```sh\n$ yarn add array-sort\n```\n\n## Usage\n\nSort an array by the given object property:\n\n```js\nvar arraySort = require('array-sort');\n\narraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');\n//=\u003e [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]\n```\n\n**Reverse order**\n\n```js\narraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});\n//=\u003e [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]\n```\n\n## Params\n\n```js\narraySort(array, comparisonArgs);\n```\n\n* `array`: **{Array}** The array to sort\n* `comparisonArgs`: **{Function|String|Array}**: One or more functions or object paths to use for sorting.\n\n## Examples\n\n**[Sort blog posts](examples/blog-posts.js)**\n\n```js\nvar arraySort = require('array-sort');\n\nvar posts = [\n  { path: 'c.md', locals: { date: '2014-01-09' } },\n  { path: 'a.md', locals: { date: '2014-01-02' } },\n  { path: 'b.md', locals: { date: '2013-05-06' } },\n];\n\n// sort by `locals.date`\nconsole.log(arraySort(posts, 'locals.date'));\n\n// sort by `path`\nconsole.log(arraySort(posts, 'path'));\n```\n\n**[Sort by multiple properties](examples/multiple-props.js)**\n\n```js\nvar arraySort = require('array-sort');\n\nvar posts = [\n  { locals: { foo: 'bbb', date: '2013-05-06' }},\n  { locals: { foo: 'aaa', date: '2012-01-02' }},\n  { locals: { foo: 'ccc', date: '2014-01-02' }},\n  { locals: { foo: 'ccc', date: '2015-01-02' }},\n  { locals: { foo: 'bbb', date: '2014-06-01' }},\n  { locals: { foo: 'aaa', date: '2014-02-02' }},\n];\n\n// sort by `locals.foo`, then `locals.date`\nvar result = arraySort(posts, ['locals.foo', 'locals.date']);\n\nconsole.log(result);\n// [ { locals: { foo: 'aaa', date: '2012-01-02' } },\n//   { locals: { foo: 'aaa', date: '2014-02-02' } },\n//   { locals: { foo: 'bbb', date: '2013-05-06' } },\n//   { locals: { foo: 'bbb', date: '2014-06-01' } },\n//   { locals: { foo: 'ccc', date: '2014-01-02' } },\n//   { locals: { foo: 'ccc', date: '2015-01-02' } } ]\n```\n\n**[Custom function](examples/custom-function.js)**\n\nIf custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)`Array.sort()` for more details.\n\n```js\nvar arr = [\n  {one: 'w', two: 'b'},\n  {one: 'z', two: 'a'},\n  {one: 'x', two: 'c'},\n  {one: 'y', two: 'd'},\n];\n\nfunction compare(prop) {\n  return function (a, b) {\n    return a[prop].localeCompare(b[prop]);\n  };\n}\n\nvar result = arraySort(arr, function (a, b) {\n  return a.two.localeCompare(b.two);\n});\n\nconsole.log(result);\n// [ { one: 'z', two: 'a' },\n//   { one: 'w', two: 'b' },\n//   { one: 'x', two: 'c' },\n//   { one: 'y', two: 'd' } ]\n```\n\n**[Multiple custom functions](examples/custom-functions.js)**\n\n```js\nvar arr = [\n  {foo: 'w', bar: 'y', baz: 'w'},\n  {foo: 'x', bar: 'y', baz: 'w'},\n  {foo: 'x', bar: 'y', baz: 'z'},\n  {foo: 'x', bar: 'x', baz: 'w'},\n];\n\n// reusable compare function\nfunction compare(prop) {\n  return function (a, b) {\n    return a[prop].localeCompare(b[prop]);\n  };\n}\n\n// the `compare` functions can be a list or array\nvar result = arraySort(arr, compare('foo'), compare('bar'), compare('baz'));\n\nconsole.log(result);\n// [ { foo: 'w', bar: 'y', baz: 'w' },\n//   { foo: 'x', bar: 'x', baz: 'w' },\n//   { foo: 'x', bar: 'y', baz: 'w' },\n//   { foo: 'x', bar: 'y', baz: 'z' } ]\n```\n\n## About\n\n### Related projects\n\n* [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value \"Use property paths (`a.b.c`) to get a nested value from an object.\")\n* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value \"Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.\")\n* [sort-asc](https://www.npmjs.com/package/sort-asc): Sort array elements in ascending order. | [homepage](https://github.com/jonschlinkert/sort-asc \"Sort array elements in ascending order.\")\n* [sort-desc](https://www.npmjs.com/package/sort-desc): Sort array elements in descending order. | [homepage](https://github.com/jonschlinkert/sort-desc \"Sort array elements in descending order.\")\n* [sort-object](https://www.npmjs.com/package/sort-object): Sort the keys in an object. | [homepage](https://github.com/doowb/sort-object \"Sort the keys in an object.\")\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n### Contributors\n\n| **Commits** | **Contributor** |  \n| --- | --- |  \n| 10 | [jonschlinkert](https://github.com/jonschlinkert) |  \n| 4  | [doowb](https://github.com/doowb) |  \n| 1  | [iamstolis](https://github.com/iamstolis) |  \n| 1  | [wkevina](https://github.com/wkevina) |  \n\n### Building docs\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n### Author\n\n**Jon Schlinkert**\n\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 11, 2017._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Farray-sort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Farray-sort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Farray-sort/lists"}