JavaScript Code

How to sort array of strings lexicographically?


Problem Statement

given an array of strings, 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