JSFiddle - React, Tailwind, and code Playground

by Gigapedia

JavaScript

Object.defineProperty(Object.prototype, 'deepVal', {
			enumerable: false,
			writable: true,
			value: function (propertyChain) {
				var levels = propertyChain.split('.');
				parent = this;
				for (var i = 0; i < levels.length; i++) {
					if (!parent[levels[i]])
						return undefined;
					parent = parent[levels[i]];
				}
				return parent;
			}
		});

		function _dynamicSortMultipleFields(attr) {
			var properties = arguments;
			return function (obj1, obj2) {
				var i = 0, result = 0, n = properties.length;
				while(result === 0 && i < n) {
					result = _dynamicSort(properties[i])(obj1, obj2);
					i++;
				}
				return result;
			};
		}
		function _dynamicSort(property) {
			var order = 1;
			if(property[0] === "-") {
				order = -1;
				property = property.substr(1);
			}
			return function (a,b) {
				var result = ((a.deepVal(property) > b.deepVal(property)) - (a.deepVal(property) < b.deepVal(property)));
				return result * order;
			}		
		}
		Object.defineProperty(Array.prototype, "sortBy", {
			enumerable: false,
			writable: true,
			value: function() {
				return this.sort(_dynamicSortMultipleFields.apply(null, arguments));
			}
		});

obj = [{ 
			a: { a: 1, b: 2, c: 3 },
			b: { a: 4, b: 5, c: 6 }
		},
		{ 
			a: { a: 3, b: 2, c: 1 },
			b: { a: 6, b: 5, c: 4 }
		}]

obj.sortBy('a.a');