Open In App

JavaScript Program to Sort Words in Alphabetical Order

In this article, we will see how to Sort Words in Alphabetical Order in JavaScript. Sorting words alphabetically is a common requirement in various applications, and preserving the original order can be crucial in certain scenarios. For this, there are various approaches described below.

Methods to sort words

We will explore every approach for sorting words in Alphabetical Order, along with understanding their basic implementations.



Approach 1: Using the Array sort() method

The sort() method sorts all the elements of the array and returns the sorted array. By default, the sort() method sorts the elements in lexicographic (alphabetical) order. We can simply pass the array of words to the sort() method to sort them alphabetically.

Syntax:



const sortedWords = wordsArray.sort();

Example: In this example, we will see the use of the sort() Method.




const words = [
    "JavaScript",
    "Program",
    "to",
    "Sort",
    "Words",
    "in",
    "Alphabetical",
    "Order",
];
  
// Sorting the input array using array.sort
const sortedWords = words.sort();
  
//Getting the sorter array output
console.log(sortedWords);

Output
[
  'Alphabetical',
  'JavaScript',
  'Order',
  'Program',
  'Sort',
  'Words',
  'in',
  'to'
]

Approach 2: Using the localeCompare() method

The localeCompare() the method compares two strings and returns a number indicating their relative order. We can use the localeCompare() the method as a custom sorting function in conjunction with the sort() method.

Syntax:

const sortedWords = wordsArray.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }));

Example: In this example, we will see the use of the localeCompare() Method.




const words = [
    "JavaScript",
    "Program",
    "to",
    "Sort",
    "Words",
    "in",
    "Alphabetical",
    "Order",
];
  
// Sorting array using localeCompare
const sortedWords = words.sort((a, b) =>
    a.localeCompare(b, undefined, { sensitivity: "base" })
);
  
// Show the sorted array output
console.log(sortedWords);

Output
[
  'Alphabetical',
  'in',
  'JavaScript',
  'Order',
  'Program',
  'Sort',
  'to',
  'Words'
]

Approach 3: Implementing a custom sorting algorithm

Implementing a sorting algorithm manually, we can use approaches like bubble sort, insertion sort, or merge sort. These algorithms compare pairs of words and swap them based on their order until the entire list is sorted.

Syntax:

function bubbleSort(wordsArray) {   // ...implementation of bubble sort } 

Example: In this example, we will see the use of a custom bubble sort function.




// Implementing bubble sort function
function bubbleSort(wordsArray) {
      
    // Getting size of array
    const length = wordsArray.length;
    for (let i = 0; i < length - 1; i++) {
        for (let j = 0; j < length - i - 1; j++) {
            if (
                wordsArray[j]
                    .localeCompare(wordsArray[j + 1], undefined, {
                    sensitivity: "base",
                }) > 0
            ) {
                // Swaping word in the array
                const temp = wordsArray[j];
                wordsArray[j] = wordsArray[j + 1];
                wordsArray[j + 1] = temp;
            }
        }
    }
    return wordsArray;
}
  
const words = [
    "JavaScript",
    "Program",
    "to",
    "Sort",
    "Words",
    "in",
    "Alphabetical",
    "Order",
];
  
const sortedWords = bubbleSort(words);
  
// Getting the sorted array output
console.log(sortedWords);

Output
[
  'Alphabetical',
  'in',
  'JavaScript',
  'Order',
  'Program',
  'Sort',
  'to',
  'Words'
]

Article Tags :