{"id":21269061,"url":"https://github.com/kktam/mergesort-obj","last_synced_at":"2026-05-22T14:17:43.407Z","repository":{"id":57294854,"uuid":"102305791","full_name":"kktam/mergesort-obj","owner":"kktam","description":"node library for merge sort, with object support","archived":false,"fork":false,"pushed_at":"2017-09-09T15:06:41.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-22T10:17:19.317Z","etag":null,"topics":["algorithm","mergesort","nodejs"],"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/kktam.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-09-04T01:22:39.000Z","updated_at":"2021-06-28T16:47:01.000Z","dependencies_parsed_at":"2022-08-30T18:51:54.526Z","dependency_job_id":null,"html_url":"https://github.com/kktam/mergesort-obj","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kktam%2Fmergesort-obj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kktam%2Fmergesort-obj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kktam%2Fmergesort-obj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kktam%2Fmergesort-obj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kktam","download_url":"https://codeload.github.com/kktam/mergesort-obj/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243725533,"owners_count":20337667,"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":["algorithm","mergesort","nodejs"],"created_at":"2024-11-21T08:07:11.411Z","updated_at":"2026-05-22T14:17:38.362Z","avatar_url":"https://github.com/kktam.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mergesort-obj [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]\n\u003e merge sort with objects support\n\n## Installation\n\n```sh\n$ npm install --save mergesort-obj\n```\n\n## Usage\n\nThe basic usage requires a user to create a custom sorting function, and passes it to the sort() method.\n\n```js\nconst merge = require('mergesort-obj');\n\n// define a sorting function\n// the sorting function should have input parameters, node1 (first node) and node2 (second node) for comparison\n// the function should resolve 3 possible outcomes, where the first node is deemed to have lesser precedence (CMP_GREATER_THAN), same precedence CMP_EQUAL, or greater precedence (CMP_LESS_THAN). Greater precedence gets placed in the beginning of the array.\nvar sortAsc = function (node1, node2) {\n  if (node1 \u003c node2) {\n    return merge.CMP_LESS_THAN;\n  } else if (node1 === node2) {\n    return merge.CMP_EQUAL;\n  }\n  return merge.CMP_GREATER_THAN;\n};\n\n// define an array\nvar elm = [3, 2, 1];\n\n// sort the array\nvar results = merge.sort(elm, sortAsc);\n\nforeach (var item in results) {\n    console.log(item);\n}\n```\n\nThe library supports objects in arrays and one can create a simple sorting function to sort based on a particular attribute in the object, such as follows:\n\n```\nvar sortDsc = function (node1, node2) {\n  if (node1.id \u003e node2.id) {\n    return merge.CMP_LESS_THAN;\n  } else if (node1.id === node2.id) {\n    return merge.CMP_EQUAL;\n  }\n  return merge.CMP_GREATER_THAN;\n};\n```\n\nThis library has a helper function, sortObj() that makes this easier. This function simplifies the use of sorting algorithm, without the need of creating a custom sorting function.\n\nFor simple objects with simple search keys, one can use the sortObj() to perform the same key based searches above, as follows:\n\n```\nvar attr = 'id';\nvar elm = [{id: 3, name: 'Albert'}, {id: 2, name: 'Denny'}, {id: 1, name: 'George'}];\n\nvar results = merge.sortObj(elm, attr, merge.DIR_ASC);\n```\n\nto use sortObj() function, provide the name of the attribute to be used as sorting key (attr), and the direction, DIR_ASC for ascending sort, and DIR_DSC for descending sort.\n\nTo further support complex sorting even more, the helper function sortObjMultiKey() is introduced to arrange sorting on multiple keys.\n\nTo use this function, one must supply first the array, and second the \"order\" definition structure. The order structure is an array of objects with the following attributes:\n\n```\n[{attr: 'name of attribute', dir: direction}, { ... } , ...];\n```\n\nattr is the exact spelling of the attribute to use as key, and dir is the direction of the search, either DIR_ASC (ascending), or DIR_DSC (descending).\n\nThe sort order is defined by order of the element position in the array, i.e. the first object in the array is the key that will be sorted first. If the algorithm cannot determine precedence with the first key, it will move the second, third, and so forth, until either all the keys have run out, or an decision has been made.\n\nThe following is an example of setting up sorting using 2 keys with mixed direction.\n\n```\n    // Order by firstname, and then lastname\n    var order = [{attr: 'firstname', dir: merge.DIR_ASC}, {attr: 'lastname', dir: merge.DIR_DSC}];\n    var elm = [{id: 1, firstname: 'George', lastname: 'Washington'}, {id: 2, firstname: 'Ronald', lastname: 'Reagan'}, {id: 3, firstname: 'Albert', lastname: 'Einstein'}, {id: 4, firstname: 'George', lastname: 'Michael'}];\n    var results = merge.sortObjMultiKey(elm, order);\n```\n\n## License\n\nMIT © [Nelson Tam]()\n\n\n[npm-image]: https://badge.fury.io/js/mergesort-obj.svg\n[npm-url]: https://npmjs.org/package/mergesort-obj\n[travis-image]: https://travis-ci.org/kktam/mergesort-obj.svg?branch=master\n[travis-url]: https://travis-ci.org/kktam/mergesort-obj\n[daviddm-image]: https://david-dm.org/kktam/mergesort-obj.svg?theme=shields.io\n[daviddm-url]: https://david-dm.org/kktam/mergesort-obj\n[coveralls-image]: https://coveralls.io/repos/kktam/mergesort-obj/badge.svg\n[coveralls-url]: https://coveralls.io/r/kktam/mergesort-obj\n# mergesort-obj\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkktam%2Fmergesort-obj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkktam%2Fmergesort-obj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkktam%2Fmergesort-obj/lists"}