2020-08-24 Sorting object keys with Lodash

I have been extremely busy recently, so I have only a short tip today. Imagine having a JavaScript object like this:

  a: 1,
  b: 3,
  c: 2,

and needing to have an (almost) identical object, but sorted by increasing values. (Yes, I know that theoretically the order of object properties does not matter and should not be relied upon, but come on.) Here is a very simple way to obtain this (using Lodash): ,

const _ = require('lodash');
const source = {
	a: 1,
	b: 3,
	c: 2,
const target = _(source)

There’s not really a lot more to say about this – we convert the object to an array, sort it, and convert back (well, I used the Lodash chaining, but this is not really necessary).

CategoryEnglish, CategoryBlog, CategoryJavaScript