10 examples of 'javascript sort array of objects by key value' in JavaScript

Every line of 'javascript sort array of objects by key value' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
49function sort(object) {
50 var sortable = [];
51 for (var key in object) {
52 sortable.push([key, object[key]]);
53 }
54
55 return sortable.sort(function(a, b) {
56 return b[1] - a[1];
57 });
58}
7function sort(object) {
8 if (sortArrays === true && Array.isArray(object)) {
9 return object.sort();
10 }
11 else if (typeof object !== "object" || object === null) {
12 return object;
13 }
14
15 return Object.keys(object).sort().map(function(key) {
16 return {
17 key: key,
18 value: sort(object[key])
19 };
20 });
21}
8function sort(object) {
9 if (sortArrays === true && Array.isArray(object)) {
10 return object.sort();
11 }
12 else if (typeof object !== "object" || object === null) {
13 return object;
14 }
15
16 return Object.keys(object).sort().map(function (key) {
17 return {
18 key: key,
19 value: sort(object[key])
20 };
21 });
22}
8export function sortObjectArray(array, sortKey, isAscending) {
9 const results = array.sort((rowDataA, rowDataB) => {
10 const a = rowDataA[sortKey];
11 const b = rowDataB[sortKey];
12 if (typeof a === 'string' && typeof b === 'string') { // If strings, sort case-insensitive
13 return a.toLowerCase().localeCompare(b.toLowerCase());
14 }
15 // If any other type of object, e.g. a number, just use the natural ordering
16 if (a < b) return -1;
17 if (a > b) return 1;
18 return 0;
19 });
20 if (!isAscending) return results.reverse();
21 return results;
22}
64function sortObject (arr) {
65 return arr.sort((a, b) => (a.index - b.index))
66}
54function sortByKey(array, key) {
55 return array.sort(function(a, b) {
56 var x = a[key];
57 var y = b[key];
58 return x < y ? -1 : x > y ? 1 : 0;
59 });
60}
54function sortObjectArray(arr: T[]) {
55 return arr.sort(compareObjects);
56}
18export function sortObject(object: any): T {
19 let sortedObj: any = {}
20 let keys = _.keys(object)
21 keys = _.sortBy(keys, (key: string) => {
22 return key
23 })
24
25 _.each(keys, (key: string) => {
26 if (typeof object[key] === 'object' && !(object[key] instanceof Array)) {
27 sortedObj[key] = sortObject(object[key])
28 } else {
29 sortedObj[key] = object[key]
30 }
31 })
32
33 return sortedObj as T
34}
141export function sort_by(array: T[], key: (item: T) => number): T[] {
142 const tmp = array.map((value, index) => {
143 return {value, index, key: key(value) }
144 })
145 tmp.sort((left, right) => {
146 const a = left.key
147 const b = right.key
148 if (a !== b) {
149 if (a > b || a === undefined) return 1
150 if (a < b || b === undefined) return -1
151 }
152 return left.index - right.index
153 })
154 return tmp.map((item) => item.value)
155}
125sortObjectByKeys(obj) {
126 if (!obj || typeof obj !== 'object') {
127 return {};
128 }
129
130 let newObj = {};
131 Object.keys(obj).sort().forEach((k) => {
132 newObj[k] = obj[k];
133 });
134 return newObj;
135}

Related snippets