From 4bc8a152c1028bc9326f146d8cd677f122a121b0 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Wed, 28 Feb 2018 23:00:41 -0800 Subject: [PATCH] Added natural sorting for documents and collections --- app/stores/CollectionsStore.js | 3 ++- package.json | 3 ++- server/presenters/collection.js | 4 ++-- shared/utils/naturalSort.js | 10 ++++++++++ yarn.lock | 4 ++++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 shared/utils/naturalSort.js diff --git a/app/stores/CollectionsStore.js b/app/stores/CollectionsStore.js index 45c4dbb9..919e96db 100644 --- a/app/stores/CollectionsStore.js +++ b/app/stores/CollectionsStore.js @@ -8,6 +8,7 @@ import stores from 'stores'; import Collection from 'models/Collection'; import ErrorsStore from 'stores/ErrorsStore'; import UiStore from 'stores/UiStore'; +import naturalSort from 'shared/utils/naturalSort'; import type { PaginationParams } from 'types'; type Options = { @@ -42,7 +43,7 @@ class CollectionsStore { @computed get orderedData(): Collection[] { - return _.sortBy(this.data.values(), 'name'); + return naturalSort(Array.from(this.data.values()), 'name'); } /** diff --git a/package.json b/package.json index dbed02ea..97c87fae 100644 --- a/package.json +++ b/package.json @@ -63,8 +63,8 @@ "dependencies": { "@tommoor/slate-drop-or-paste-images": "^0.8.1", "@tommoor/slate-edit-code": "^0.13.3", - "aws-sdk": "^2.135.0", "autotrack": "^2.4.1", + "aws-sdk": "^2.135.0", "babel-core": "^6.24.1", "babel-eslint": "^8.1.2", "babel-loader": "^7.1.2", @@ -136,6 +136,7 @@ "mobx-react": "^4.1.8", "mobx-react-devtools": "^4.2.11", "moment": "2.13.0", + "natural-sort": "^1.0.0", "node-dev": "3.1.0", "nodemailer": "^4.4.0", "normalize.css": "^7.0.0", diff --git a/server/presenters/collection.js b/server/presenters/collection.js index 89dfe11e..965aad8c 100644 --- a/server/presenters/collection.js +++ b/server/presenters/collection.js @@ -1,7 +1,7 @@ // @flow -import _ from 'lodash'; import { Collection } from '../models'; import presentDocument from './document'; +import naturalSort from '../../shared/utils/naturalSort'; type Document = { children: Document[], @@ -11,7 +11,7 @@ type Document = { }; const sortDocuments = (documents: Document[]) => { - const orderedDocs = _.sortBy(documents, ['title']); + const orderedDocs = naturalSort(documents, 'title'); return orderedDocs.map(document => ({ ...document, children: sortDocuments(document.children), diff --git a/shared/utils/naturalSort.js b/shared/utils/naturalSort.js new file mode 100644 index 00000000..3cfb0a25 --- /dev/null +++ b/shared/utils/naturalSort.js @@ -0,0 +1,10 @@ +// @flow + +import _ from 'lodash'; +import naturalSort from 'natural-sort'; + +export default (sortableArray: Object[], key: string) => { + let keys = sortableArray.map(object => object[key]); + keys.sort(naturalSort()); + return _.sortBy(sortableArray, object => keys.indexOf(object[key])); +}; diff --git a/yarn.lock b/yarn.lock index 1024d6ff..406c66d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6628,6 +6628,10 @@ natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" +natural-sort@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/natural-sort/-/natural-sort-1.0.0.tgz#eac301fd86c268a771222c62dc7719402aa34380" + ncname@1.0.x: version "1.0.0" resolved "https://registry.yarnpkg.com/ncname/-/ncname-1.0.0.tgz#5b57ad18b1ca092864ef62b0b1ed8194f383b71c"