Skip to content

Instantly share code, notes, and snippets.

@gustavoguichard
Last active July 6, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavoguichard/be74eebb0ccbc8e90064ceac7232d267 to your computer and use it in GitHub Desktop.
Save gustavoguichard/be74eebb0ccbc8e90064ceac7232d267 to your computer and use it in GitHub Desktop.
import compact from 'lodash/compact'
import get from 'lodash/get'
import head from 'lodash/head'
import orderBy from 'lodash/orderBy'
const getFirstValue = (el, path) => {
const paths = path.split('|')
const values = paths.map(path => get(el, path))
return head(compact(values))
}
const padStringWithLeadingNumbers = (str, size = 9) => {
const matches = String(str).match(/^(\d+)(.+)?/)
if (matches) {
const [_, num, word] = matches
const largenum = num.padStart(size, '0')
return [largenum, word].join('')
}
return str
}
const naturalSortingValue = el => {
const str = getFirstValue(el, 'customFieldValues.sorting|name')
return padStringWithLeadingNumbers(str, 9)
}
orderBy([], naturalSortingValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment