add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.sortBy


The lodash library is a popular utility library for JavaScript that provides many helpful methods and functions for everyday programming tasks. One such method is the _.sortBy method, which allows developers to sort collections of data in a custom way.

The _.sortBy method is defined as a function that takes two arguments: the collection of data to sort and the function that defines the sort order. The function that defines the sort order takes one argument and returns a value which is used for comparison.

For example, let's say we have an array of objects representing people's names and ages:

let people = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 20 }

If we want to sort this array by age, we can use the _.sortBy method like so:

let sortedPeople = _.sortBy(people, (person) => person.age);

In this example, we pass the people array as the first argument to _.sortBy, and a function that takes each person object as an argument and returns their age property as the value used for comparison.

The resulting sortedPeople array will be:

  { name: 'Charlie', age: 20 },
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },

We can also pass multiple functions as arguments to _.sortBy to define a more complex sorting order. The functions will be applied in the order they are passed.

let sortedPeople = _.sortBy(people, 
  (person) => person.age, 
  (person) =>

In this example, we first sort by age and then sort by name within each group of people with the same age.

The _.sortBy method can also be used to sort arrays of strings or numbers by passing a simple function that returns the value to use for comparison.

let letters = ['d', 'a', 'c', 'b'];
let sortedLetters = _.sortBy(letters, (letter) => letter);

// sortedLetters = ['a', 'b', 'c', 'd']

let numbers = [10, 30, 50, 20];
let sortedNumbers = _.sortBy(numbers, (number) => number);

// sortedNumbers = [10, 20, 30, 50]

Overall, the _.sortBy method is a powerful tool for sorting collections of data in JavaScript, providing developers with a flexible way to define custom sorting criteria.