UNPKG

supersortjs

Version:

Cocktail sort with using compare context function

42 lines (30 loc) 1.01 kB
#What is this? Get super sort function for you needs. You should use own compare function. #Installation `npm i supersort --save` #Then... ``` const sort = require("supersortjs") const obj1 = {field : 3,} const obj2 = {field : 1,} const obj3 = {field : 3,} const obj4 = {field : -13} const array = [obj1,obj2,obj3,obj4] const context = { compareObjectField: function (value1, value2) { if (value1.field > value2.field) return -1 if (value2.field < value1.field) return 1 if (value2.field === value1.field) return 1 } } const sorted = sort(array, function (v1,v2) { return this.compareObjectField(v1,v2) }, context) console.log(sorted); ``` #Options sort function accept 3 arguments, third is optional. * arr - Array that need to sort * compare - Function that compare array numbers. Should return -1, if left operand bigger than right, and 1, if operand are equal or right bigger. * context - (optional) Context of using compare function.