{"id":12693,"date":"2022-10-03T15:29:33","date_gmt":"2022-10-03T19:29:33","guid":{"rendered":"http:\/\/local.brightwhiz\/?post_type=snippets&p=12693"},"modified":"2022-10-03T15:29:36","modified_gmt":"2022-10-03T19:29:36","slug":"how-to-sort-string-alphabetically-in-javascript","status":"publish","type":"snippets","link":"http:\/\/local.brightwhiz\/snippets\/how-to-sort-string-alphabetically-in-javascript\/","title":{"rendered":"How to Sort String Alphabetically in JavaScript"},"content":{"rendered":"\n
Use this to sort string alphabetically in JavaScript:<\/p>\n\n\n\n
arr.sort(function(a, b) {\n return a === b ? 0 : a < b ? -1 : 1;\n});\n\nfunction sortAlphabeticalOrder(arr) {\n return arr.sort((a, b) => a < b ? -1 : 1)\n}\n\nsortAlphabeticalOrder(["a", "d", "c", "a", "z", "g"]);\n\n\/\/ returns:\n\/\/ [ 'a', 'a', 'c', 'd', 'g', 'z' ]<\/code><\/pre>\n","protected":false},"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[953,23,27,15,16],"tags":[320,328,433,471,635,638],"yoast_head":"\nHow to Sort String Alphabetically in JavaScript [Snippet]<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n