Skip to content

Commit 50ac230

Browse files
author
Alexander Anpleenko
committed
publish
1 parent 73c41af commit 50ac230

File tree

8 files changed

+154
-0
lines changed

8 files changed

+154
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015", "stage-0", "react"],
3+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
dist/
3+
npm-debug.log
4+
yarn.lock

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.npmignore
2+
.gitignore
3+
Makefile
4+
node_modules/
5+
.babelrc
6+
npm-debug.log
7+
yarn.lock
8+
.travis.yml
9+
.editorconfig
10+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Alexander
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
prepublish:
2+
rm -rf ./dist
3+
babel index.js --out-dir ./dist

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
# sort-multidimensional-array-func
2+
3+
### multidimensional array sorting function
4+
5+
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url]
6+
7+
## Install
8+
9+
```
10+
npm install --save sort-multidimensional-array-func
11+
```
12+
13+
## Usage
14+
15+
```javascript
16+
import sortMultidimensionalArrayFunc from 'sort-multidimensional-array-func';
17+
18+
const arr = [
19+
[123, 345, 545],
20+
[456, 564, 345],
21+
[12, 3, 4]
22+
]
23+
24+
console.log(sortMultidimensionalArrayFunc(arr, 1, 'asc'))
25+
// [[12, 3, 4], [123, 345, 545], [456, 564, 345]]
26+
27+
console.log(sortMultidimensionalArrayFunc(arr, 1, 'desc'))
28+
// [[456, 564, 345], [123, 345, 545], [12, 3, 4]]
29+
30+
console.log(sortMultidimensionalArrayFunc(arr, 2, 'desc'))
31+
// [[123, 345, 545], [456, 564, 345], [12, 3, 4]]
32+
```
33+
34+
[downloads-image]: https://img.shields.io/npm/dm/sort-multidimensional-array-func.svg
35+
[npm-url]: https://www.npmjs.com/package/sort-multidimensional-array-func
36+
[npm-image]: https://img.shields.io/npm/v/sort-multidimensional-array-func.svg

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {array} Source array
3+
* @param {number} Sort index
4+
* @param {string} Sort method
5+
*/
6+
const sortMultidimensionalArrayFunc = (arr = [], index = 0, orderBy) => {
7+
if (process.env.NODE_ENV !== 'production') {
8+
function classof(obj) {
9+
return Object.prototype.toString.call(obj).slice(8, -1);
10+
}
11+
12+
if(classof(arr) !== 'Array') {
13+
throw new Error('First argument must be a array');
14+
}
15+
16+
if(classof(index) !== 'Number') {
17+
throw new Error('Second argument must be a number');
18+
}
19+
}
20+
21+
function asc(firstArray, secondArray) {
22+
if (firstArray[index] > secondArray[index]) {
23+
return 1;
24+
} else if (firstArray[index] < secondArray[index]) {
25+
return -1;
26+
}
27+
28+
return 0;
29+
}
30+
31+
function desc(firstArray, secondArray) {
32+
if (firstArray[index] < secondArray[index]) {
33+
return 1;
34+
} else if (firstArray[index] > secondArray[index]) {
35+
return -1;
36+
}
37+
38+
return 0;
39+
}
40+
41+
switch (orderBy) {
42+
case 'asc': return arr.sort(asc);
43+
case 'desc': return arr.sort(desc);
44+
default: return arr;
45+
}
46+
};
47+
48+
export default sortMultidimensionalArrayFunc;

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "sort-multidimensional-array-func",
3+
"version": "1.0.0",
4+
"description": "multidimensional array sorting function",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"prepublish": "make prepublish"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/vaeum/sort-multidimensional-array-func.git"
12+
},
13+
"keywords": [
14+
"gauss",
15+
"round",
16+
"sort-multidimensional-array-func"
17+
],
18+
"author": "vaeum",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/vaeum/sort-multidimensional-array-func/issues"
22+
},
23+
"homepage": "https://github.com/vaeum/sort-multidimensional-array-func#readme",
24+
"devDependencies": {
25+
"babel-core": "^6.21.0",
26+
"babel-preset-es2015": "^6.18.0",
27+
"babel-preset-react": "^6.16.0",
28+
"babel-preset-stage-0": "^6.16.0"
29+
}
30+
}

0 commit comments

Comments
 (0)