Installations
npm install @bemoje/arr-sort-comparator
Developer Guide
Typescript
No
Module System
CommonJS, UMD
Node Version
12.13.0
NPM Version
6.12.1
Score
71
Supply Chain
53.8
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (98.05%)
Shell (1.95%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
bemoje
Download Statistics
Total Downloads
2,288
Last Day
1
Last Week
1
Last Month
30
Last Year
270
GitHub Statistics
1 Stars
6 Commits
2 Watching
1 Branches
1 Contributors
Bundle Size
26.68 kB
Minified
9.15 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.5
Package Id
@bemoje/arr-sort-comparator@1.0.5
Unpacked Size
26.70 kB
Size
6.41 kB
File Count
5
NPM Version
6.12.1
Node Version
12.13.0
Total Downloads
Cumulative downloads
Total Downloads
2,288
Last day
0%
1
Compared to previous day
Last week
-92.3%
1
Compared to previous week
Last month
-36.2%
30
Compared to previous month
Last year
-32.2%
270
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
6
@bemoje/arr-sort-comparator
Create array comparator function.
Version
Travis CI
Dependencies
Stats
Donate
Installation
1npm install @bemoje/arr-sort-comparator 2npm install --save @bemoje/arr-sort-comparator 3npm install --save-dev @bemoje/arr-sort-comparator
Usage
1 2import compare from '@bemoje/arr-sort-comparator' 3 4let arr 5 6/** 7 * DATA: STRINGS 8 * -------------- 9 */ 10 11arr = ['5', '2', '4', '30', '1', '3'] 12 13/** 14 * SORT ALPHABETICALLY BY DEFAULT 15 * ------------------------------ 16 */ 17 18arr.sort(compare()) 19//=> ['1', '2', '3', '30', '4', '5'] 20 21/** 22 * DATA: NUMERIC VALUES 23 * ---------------------- 24 */ 25 26arr = [5, 2, 4, 30, 1, 3] 27 28/** 29 * SORT NUMERICALLY 30 * ---------------- 31 */ 32 33arr.sort( 34 compare({ 35 numeric: true, 36 }), 37) 38//=> [1, 2, 3, 4, 5, 30] 39 40/** 41 * SORT DESCENDING 42 * --------------- 43 */ 44 45arr.sort( 46 compare({ 47 numeric: true, 48 descending: true, 49 }), 50) 51//=> [30, 5, 4, 3, 2, 1] 52 53/** 54 * DATA: PERSON OBJECTS 55 * -------------------- 56 */ 57 58arr = [ 59 { name: 'john', age: 4 }, 60 { name: 'bill', age: 8 }, 61] 62 63/** 64 * SORT OBJECTS BY PROPERTY 65 * ------------------------ 66 */ 67 68arr.sort( 69 compare({ 70 by: 'name', 71 }), 72) 73 74/* => 75 [ 76 { name: 'bill', age: 8 }, 77 { name: 'john', age: 4 }, 78 ] 79*/ 80 81arr.sort( 82 compare({ 83 numeric: true, 84 by: 'age', 85 }), 86) 87 88/* => 89 [ 90 { name: 'john', age: 4 }, 91 { name: 'bill', age: 8 }, 92 ] 93*/ 94 95/** 96 * DATA: PERSON OBJECTS WITH NESTED NAME OBJECTS 97 * --------------------------------------------- 98 */ 99 100arr = [ 101 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 102 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 103] 104 105/** 106 * SORT OBJECTS BY NESTED PROPERTY WITH DOT NOTATION 107 * ------------------------------------------------- 108 */ 109 110arr.sort( 111 compare({ 112 by: 'name.first', 113 }), 114) 115 116/* => 117 [ 118 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 119 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 120 ] 121*/ 122 123arr.sort( 124 compare({ 125 by: 'name.last', 126 }), 127) 128 129/* => 130 [ 131 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 132 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 133 ] 134*/ 135 136/** 137 * DATA: STRING DIRECTORY PATHS SPLIT IN ARRAYS 138 * -------------------------------------------- 139 */ 140 141arr = [ 142 ['repo', 'src', 'compare.js'], 143 ['repo', 'docs', 'index.html'], 144] 145 146/** 147 * SORT BY ARRAY INDEX 148 * ------------------- 149 */ 150 151arr.sort( 152 compare({ 153 by: 2, 154 }), 155) 156 157/* => 158 [ 159 ['repo', 'src', 'compare.js'], 160 ['repo', 'docs', 'index.html'], 161 ] 162*/ 163 164arr.sort( 165 compare({ 166 by: 1, 167 }), 168) 169 170/* => 171 [ 172 ['repo', 'docs', 'index.html' ], 173 ['repo', 'src', 'compare.js'], 174 ] 175*/ 176 177/** 178 * DATA: DIRECTORY PATHS ARRAYS WITH SUB-ARRAYS 179 * -------------------------------------------- 180 */ 181 182arr = [ 183 ['repo', 'src', ['compare', 'json']], 184 ['repo', 'src', ['compare', 'ts']], 185 ['repo', 'src', ['compare', 'js']], 186] 187 188/** 189 * SORT ARRAYS AND SUB-ARRAYS RECURSIVELY 190 * ------------------------------------ 191 */ 192 193arr.sort( 194 compare({ 195 arrays: true, 196 }), 197) 198 199/* => 200 [ 201 ['repo', 'src', ['compare', 'js']], 202 ['repo', 'src', ['compare', 'json']], 203 ['repo', 'src', ['compare', 'ts']], 204 ] 205*/ 206 207/** 208 * DATA: IP ADDRESSES AS ARRAYS 209 * ---------------------------- 210 */ 211 212arr = [ 213 [192, 168, 0, 1], 214 [192, 168, 0, 101], 215 [172, 0, 0, 1], 216] 217 218/** 219 * SORT NUMERIC IP-ADDRESSES AS ARRAYS 220 * ----------------------------------- 221 */ 222 223arr.sort( 224 compare({ 225 numeric: true, 226 arrays: true, 227 }), 228) 229 230/* => 231 [ 232 [172, 0, 0, 1], 233 [192, 168, 0, 1], 234 [192, 168, 0, 101], 235 ] 236*/ 237 238/** 239 * DATA: USER CLASS INSTANCES 240 * -------------------------- 241 */ 242 243class User { 244 constructor(firstName, lastName) { 245 this.firstName = firstName 246 this.lastName = lastName 247 } 248 get fullName() { 249 return this.firstName + ' ' + this.lastName 250 } 251} 252 253arr = [ 254 new User('john', 'doe'), 255 new User('peter', 'wick'), 256 new User('peter', 'johnson'), 257 new User('les', 'paul'), 258] 259 260/** 261 * SORT BY GETTER-FUNCTION 262 * ------------------------ 263 */ 264 265arr.sort( 266 compare({ 267 by: (user) => { 268 return user.fullName 269 }, 270 }), 271) 272 273/* => 274 [ 275 { firstName: 'john', lastName: 'doe'}, 276 { firstName: 'les', lastName: 'paul'}, 277 { firstName: 'peter', lastName: 'johnson'}, 278 { firstName: 'peter', lastName: 'wick'}, 279 ] 280*/ 281
Tests
Uses Jest to test module functionality. Run tests to get coverage details.
1npm run test
API
Table of Contents
Create array comparator function.
Parameters
-
options
object?-
options.numeric
boolean Sort numerically. Defaults to lexicographic/alphabetic sort. (optional, defaultfalse
) -
options.descending
boolean Sort in descending order. Defaults to ascending order. (optional, defaultfalse
) -
options.array
boolean Sort arrays. Nested arrays are also compared recursively. (optional, defaultfalse
) -
options.by
(number | string | getter) Sort by either array index, a callback(element): any - or by object keys with dot-notation support. (optional, defaultundefined
)
-
Returns comparator
comparator
Type: Function
Parameters
-
a
any The first value to compare -
b
any The second value to compare
Returns number A negative number if a > b, a positive number if a < b, 0 otherwise.
getter
Type: Function
Parameters
a
any The value
Returns any The value to be compared
sortNumeric
Numerical comparison of items. If the passed objects are not numbers, their .valueOf() methods are called to retrieve a numeric value representation of them.
Parameters
Returns number A positive number if a > b, a negative number if a < b, 0 otherwise.
sortAlpha
Alphabetical comparison of items.
Parameters
Returns number A positive number if a.toString() > b.toString(), a negative number if .toString() < b.toString(), 0 otherwise.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/6 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
53 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-wg6g-ppvx-927h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-vfrc-7r7c-w9mx
- Warn: Project is vulnerable to: GHSA-7wwv-vh3v-89cq
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-3j8f-xvm3-ffx4
- Warn: Project is vulnerable to: GHSA-4p35-cfcx-8653
- Warn: Project is vulnerable to: GHSA-7f3x-x4pr-wqhj
- Warn: Project is vulnerable to: GHSA-jpp7-7chh-cf67
- Warn: Project is vulnerable to: GHSA-q6wq-5p59-983w
- Warn: Project is vulnerable to: GHSA-j9fq-vwqv-2fm2
- Warn: Project is vulnerable to: GHSA-pqw5-jmp5-px4v
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-9q5w-79cv-947m
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-w5p7-h5w8-2hfq
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-g78m-2chm-r7qv
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.7
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to @bemoje/arr-sort-comparator
@bemoje/arr-sort
Sort an array considerably faster than the native Array.prototype.sort as a drop-in replacement. Fork of of the famous timsort module, but this module allows for passing comparator-builder options instead of a comparator function. In short, advanced compa
@bemoje/arr-sorted-add
For a sorted array, add an element. Whichever comparator function was used to sort the array, can be passed. Also supports comparator-builder options. For reference, see: https://github.com/bemoje/bemoje-arr-sort-comparator