{"id":16644169,"url":"https://github.com/75lb/sort-array","last_synced_at":"2025-04-07T08:23:38.033Z","repository":{"id":34884237,"uuid":"38901963","full_name":"75lb/sort-array","owner":"75lb","description":"Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order","archived":false,"fork":false,"pushed_at":"2024-08-29T23:01:10.000Z","size":514,"stargazers_count":43,"open_issues_count":3,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-12T08:10:52.370Z","etag":null,"topics":["array","esmodules","function","isomorphic","javascript","javascript-library","load-anywhere","module","sort-array"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"golang/mobile","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/75lb.png","metadata":{"files":{"readme":"README.hbs","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":"2015-07-10T21:02:44.000Z","updated_at":"2024-09-23T23:55:02.000Z","dependencies_parsed_at":"2024-06-18T13:55:57.971Z","dependency_job_id":"6557893d-2eeb-4767-8bb8-36619abcb192","html_url":"https://github.com/75lb/sort-array","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/75lb%2Fsort-array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/75lb%2Fsort-array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/75lb%2Fsort-array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/75lb%2Fsort-array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/75lb","download_url":"https://codeload.github.com/75lb/sort-array/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247616043,"owners_count":20967316,"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","esmodules","function","isomorphic","javascript","javascript-library","load-anywhere","module","sort-array"],"created_at":"2024-10-12T08:10:39.070Z","updated_at":"2025-04-07T08:23:37.993Z","avatar_url":"https://github.com/75lb.png","language":"JavaScript","readme":"[![view on npm](https://badgen.net/npm/v/sort-array)](https://www.npmjs.org/package/sort-array)\n[![npm module downloads](https://badgen.net/npm/dt/sort-array)](https://www.npmjs.org/package/sort-array)\n[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/sort-array)](https://github.com/75lb/sort-array/network/dependents?dependent_type=REPOSITORY)\n[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/sort-array)](https://github.com/75lb/sort-array/network/dependents?dependent_type=PACKAGE)\n[![Node.js CI](https://github.com/75lb/sort-array/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/sort-array/actions/workflows/node.js.yml)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)\n\n# sort-array\n\nIsomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.\n\n```js\nconst sortArray = require('sort-array')\n```\n\n## Synopsis\n\nSome trivial examples to demonstrate typical usage.\n\n### Sorting an array of primitives\n\n#### Ascending order\n\nSort an array of strings in ascending order (the default).\n\n```js\n\u003e const partsOfTheDay = ['twilight', 'afternoon', 'morning', 'evening']\n\n\u003e sortArray(partsOfTheDay)\n[ 'afternoon', 'evening', 'morning', 'twilight' ]\n```\n\n#### Descending order\n\nSort an array of strings in descending order.\n\n```js\n\u003e sortArray(partsOfTheDay, { order: 'desc' })\n[ 'twilight', 'morning', 'evening', 'afternoon' ]\n```\n\n#### Custom sort order\n\nThe default value for `options.order` is `'asc'`. You can also specify `'desc'` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.\n\n```js\n\u003e sortArray(partsOfTheDay, {\n  order: 'time',\n  customOrders: {\n    time: ['morning', 'afternoon', 'evening', 'twilight']\n  }\n})\n[ 'morning', 'afternoon', 'evening', 'twilight' ]\n```\n\n### Sorting an array of objects\n\n#### Sort by object property\n\nPass one or more property names to `options.by` to sort an array of objects by those properties.\n\n```js\n\u003e const repositories = [\n  { name: '75lb/sort-array', openIssues: 0, closedIssues: 4 },\n  { name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },\n  { name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 }\n]\n\n\u003e sortArray(repositories, {\n  by: 'openIssues',\n  order: 'desc'\n})\n[\n  { name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },\n  { name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 },\n  { name: '75lb/sort-array', openIssues: 0, closedIssues: 4 }\n]\n```\n\n#### Sort by computed field\n\nSort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the `options.computed` object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by `total` (the name of the computed field supplied in `options.computed`).\n\n```js\n\u003e const repositories = [\n  { name: '75lb/sort-array', openIssues: 0, closedIssues: 4 },\n  { name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },\n  { name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 }\n]\n\n\u003e sortArray(repositories, {\n  by: 'total',\n  order: 'desc',\n  computed: {\n    total: repository =\u003e repository.openIssues + repository.closedIssues\n  }\n})\n[\n  { name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },\n  { name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 },\n  { name: '75lb/sort-array', openIssues: 0, closedIssues: 4 }\n]\n```\n\n#### Sort by deep object values\n\nYou can use computed fields to sort by values deep in an object structure.\n\n```js\n\u003e const data = [\n  { inner: { number: 2 } },\n  { inner: { number: 3 } },\n  { inner: { number: 1 } }\n]\n\n\u003e sortArray(data, {\n  by: 'number',\n  computed: {\n    number: row =\u003e row.inner.number\n  }\n})\n[\n  { inner: { number: 1 } },\n  { inner: { number: 2 } },\n  { inner: { number: 3 } }\n]\n```\n\n#### Sort by multiple fields\n\nSort by multiple columns using multiple custom orders.\n\n```js\n\u003e const attributes = [\n  { skill: 'accuracy', confidence: 'medium' },\n  { skill: 'power', confidence: 'high' },\n  { skill: 'speed', confidence: 'low' },\n  { skill: 'power', confidence: 'low' },\n  { skill: 'speed', confidence: 'high' },\n  { skill: 'accuracy', confidence: 'low' },\n  { skill: 'speed', confidence: 'medium' },\n  { skill: 'accuracy', confidence: 'high' },\n  { skill: 'power', confidence: 'medium' }\n]\n\n\u003e sortArray(attributes, {\n  by: ['skill', 'confidence'],\n  order: ['skill', 'confidence'],\n  customOrders: {\n    skill: ['accuracy', 'speed', 'power'],\n    confidence: ['low', 'medium', 'high'],\n  }\n})\n[\n  { skill: 'accuracy', confidence: 'low' },\n  { skill: 'accuracy', confidence: 'medium' },\n  { skill: 'accuracy', confidence: 'high' },\n  { skill: 'speed', confidence: 'low' },\n  { skill: 'speed', confidence: 'medium' },\n  { skill: 'speed', confidence: 'high' },\n  { skill: 'power', confidence: 'low' },\n  { skill: 'power', confidence: 'medium' },\n  { skill: 'power', confidence: 'high' }\n]\n```\n\nPlease visit [the sort-array wiki](https://github.com/75lb/sort-array/wiki) for more examples.\n\n## API Reference\n\n{{\u003emain}}\n\n## Load anywhere\n\nThis library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.\n\nNode.js:\n\n```js\nconst sortArray = require('sort-array')\n```\n\nWithin Node.js with ECMAScript Module support enabled:\n\n```js\nimport sortArray from 'sort-array'\n```\n\nWithin an modern browser ECMAScript Module:\n\n```js\nimport sortArray from './node_modules/sort-array/dist/index.mjs'\n```\n\nOld browser (adds `window.sortArray`):\n\n```html\n\u003cscript nomodule src=\"./node_modules/sort-array/dist/index.js\"\u003e\u003c/script\u003e\n```\n\n* * *\n\n\u0026copy; 2015-24 Lloyd Brookes \\\u003c75pound@gmail.com\\\u003e.\n\nTested by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F75lb%2Fsort-array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F75lb%2Fsort-array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F75lb%2Fsort-array/lists"}