JavaScript Code


How to sort array of strings lexicographically in JavaScript?


Sort array of strings lexicographically in JavaScript

In this tutorial, you are given an array of strings. You should write a JavaScript program to sort the array in ascending order lexicographically (order of english dictionary).

Solution

To sort an array of strings in ascending order, use array sort() method. sort() method sorts the array of strings lexicographically in ascending order, by default.

Program

fruits is the given array of strings. We use array sort() to sort strings lexicographically in ascending order.

let fruits = ['cherry', 'apple', 'banana'];
fruits.sort();
console.log(fruits);


copyright @2022