Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 5d1d0fe

Browse files
author
Kent C. Dodds
authored
feat(dynamicSort): Add sortObjectsArray method
Closes #28
2 parents 038dc6d + 6078abb commit 5d1d0fe

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import snakeToCamel from './snake-to-camel'
44
import padLeft from './pad-left'
55
import randomInteger from './random-integer'
66
import arrayFill from './array-fill'
7+
import sortObjectsArray from './sort-objects-array'
78

89
export {
910
flatten,
@@ -12,4 +13,5 @@ export {
1213
padLeft,
1314
randomInteger,
1415
arrayFill,
16+
sortObjectsArray,
1517
}

src/sort-objects-array.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default sortObjectsArray
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/4760279/3142309
5+
*
6+
* This method returns a function that can be passed into Array.sort to sort an array of objects by a given property.
7+
* Prepending "-" to the property argument sorts the array in descending order.
8+
*
9+
* @param {String} property - The property to sort by
10+
* @return {Function} - A function that can be passed into Array.sort
11+
*/
12+
function sortObjectsArray(property) {
13+
let sortOrder = 1
14+
if (property[0] === '-') {
15+
sortOrder = -1
16+
property = property.substr(1)
17+
}
18+
return function compareFunction(a, b) {
19+
let result = 0
20+
if (a[property] < b[property]) {
21+
result = -1
22+
} else if (a[property] > b[property]) {
23+
result = 1
24+
}
25+
return result * sortOrder
26+
}
27+
}

test/sort-objects-array.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import test from 'ava'
2+
import {sortObjectsArray} from '../src'
3+
4+
test('sort an array of objects by ascending order', t => {
5+
const original = [
6+
{Name: "Name", Surname: "Surname"},
7+
{Name:"AAA", Surname:"ZZZ"},
8+
{Name: "Name", Surname: "AAA"}
9+
]
10+
const expected = [
11+
{Name: "Name", Surname: "AAA"},
12+
{Name: "Name", Surname: "Surname"},
13+
{Name:"AAA", Surname:"ZZZ"}
14+
]
15+
const actual = original.slice(0).sort(sortObjectsArray('Surname'))
16+
t.same(actual, expected)
17+
})
18+
19+
test('sort an array of objects by descending order', t => {
20+
const original = [
21+
{Name: "Name", Surname: "Surname"},
22+
{Name:"AAA", Surname:"ZZZ"},
23+
{Name: "Name", Surname: "AAA"}
24+
]
25+
const expected = [
26+
{Name:"AAA", Surname:"ZZZ"},
27+
{Name: "Name", Surname: "Surname"},
28+
{Name: "Name", Surname: "AAA"}
29+
]
30+
const actual = original.slice(0).sort(sortObjectsArray('-Surname'))
31+
t.same(actual, expected)
32+
})
33+
34+
test('will not sort the an array if the property doesn\'t exist', t => {
35+
const original = [
36+
{Name: "Name", Surname: "Surname"},
37+
{Name:"AAA", Surname:"ZZZ"},
38+
{Name: "Name", Surname: "AAA"}
39+
]
40+
const actual = original.slice(0).sort(sortObjectsArray('Middle'))
41+
t.same(actual, original)
42+
})
43+

0 commit comments

Comments
 (0)