{"id":15743741,"url":"https://github.com/doowb/default-compare","last_synced_at":"2025-05-08T16:56:10.705Z","repository":{"id":48951857,"uuid":"103161265","full_name":"doowb/default-compare","owner":"doowb","description":"Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also allows sorting by an object property.","archived":false,"fork":false,"pushed_at":"2021-08-13T00:05:43.000Z","size":11,"stargazers_count":3,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T17:44:49.163Z","etag":null,"topics":["array","array-sort","compare","comparison","nodejs","sort","sorting-algorithms"],"latest_commit_sha":null,"homepage":"https://github.com/doowb/default-compare","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/doowb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-11T16:38:24.000Z","updated_at":"2021-04-12T21:55:00.000Z","dependencies_parsed_at":"2022-09-10T22:50:52.949Z","dependency_job_id":null,"html_url":"https://github.com/doowb/default-compare","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fdefault-compare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fdefault-compare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fdefault-compare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doowb%2Fdefault-compare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doowb","download_url":"https://codeload.github.com/doowb/default-compare/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252940903,"owners_count":21828769,"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","array-sort","compare","comparison","nodejs","sort","sorting-algorithms"],"created_at":"2024-10-04T03:21:16.276Z","updated_at":"2025-05-08T16:56:10.680Z","avatar_url":"https://github.com/doowb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# default-compare [![NPM version](https://img.shields.io/npm/v/default-compare.svg?style=flat)](https://www.npmjs.com/package/default-compare) [![NPM monthly downloads](https://img.shields.io/npm/dm/default-compare.svg?style=flat)](https://npmjs.org/package/default-compare)  [![NPM total downloads](https://img.shields.io/npm/dt/default-compare.svg?style=flat)](https://npmjs.org/package/default-compare) [![Linux Build Status](https://img.shields.io/travis/doowb/default-compare.svg?style=flat\u0026label=Travis)](https://travis-ci.org/doowb/default-compare) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/default-compare.svg?style=flat\u0026label=AppVeyor)](https://ci.appveyor.com/project/doowb/default-compare)\n\n\u003e Basic sort algorithm that has similar behavior to Array.prototype.sort for null and undefined, but also allows sorting by an object property.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save default-compare\n```\n\nInstall with [yarn](https://yarnpkg.com):\n\n```sh\n$ yarn add default-compare\n```\n\n## Usage\n\n```js\nvar defaultCompare = require('default-compare');\n```\n\n**basic array**\n\n```js\nvar arr = ['c', 'a', undefined, 'b', 'd', null, 'e'];\nconsole.log(arr.sort(defaultCompare));\n//=\u003e ['a', 'b', 'c', 'd', 'e', null, undefined]\n```\n\n**objects sorted by their \"name\" property**\n\n```js\nvar arr = [\n  {name: 'c', title: 'C'},\n  {name: 'a', title: 'A'},\n  {title: 'G'},\n  {name: 'b', title: 'B'},\n  {name: 'd', title: 'D'},\n  {name: null, title: 'F'},\n  {name: 'e', title: 'E'}\n];\n\narr.sort(function(a, b) {\n  return defaultCompare(a, b, 'name');\n});\n\nconsole.log(arr);\n//=\u003e [\n//=\u003e   {name: 'a', title: 'A'},\n//=\u003e   {name: 'b', title: 'B'},\n//=\u003e   {name: 'c', title: 'C'},\n//=\u003e   {name: 'd', title: 'D'},\n//=\u003e   {name: 'e', title: 'E'},\n//=\u003e   {name: null, title: 'F'},\n//=\u003e   {title: 'G'}\n//=\u003e ];\n```\n\n## API\n\n### [defaultCompare](index.js#L16)\n\nBasic sort algorithm that has similar behavior to `Array.prototype.sort`\nfor null and undefined, but also allows sorting by an object property.\n\n**Params**\n\n* `a` **{Mixed}**: First value to compare.\n* `b` **{Mixed}**: Second value to compare.\n* `prop` **{String}**: Optional property to use when comparing objects. If specified must be a string.\n* `returns` **{Number}**: Returns 1 when `a` should come after `b`, -1 when `a` should come before `b`, and 0 when `a` and `b` are equal.\n\n## About\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\nPlease read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.\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**Brian Woodward**\n\n* [github/doowb](https://github.com/doowb)\n* [twitter/doowb](https://twitter.com/doowb)\n\n### License\n\nCopyright © 2017, [Brian Woodward](https://doowb.com).\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%2Fdoowb%2Fdefault-compare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoowb%2Fdefault-compare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoowb%2Fdefault-compare/lists"}