10 examples of 'javascript sort array of objects alphabetically' in JavaScript

Every line of 'javascript sort array of objects alphabetically' 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}
54function sortObjectArray(arr: T[]) {
55 return arr.sort(compareObjects);
56}
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}
64function sortObject (arr) {
65 return arr.sort((a, b) => (a.index - b.index))
66}
20export function sort(array: T[]): T[] {
21 return array.sort((a, b) => (a.sortId === undefined || b.sortId === undefined) ? 0 : a.sortId - b.sortId);
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}
229function sort(array) { return array.sort(sortFn); }
716function sort(array) {
717 return array.sort(sortFn);
718}
1export function sort(arr) {
2 return [...arr].sort((a, b) => a - b);
3}

Related snippets