Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case
Next article icon

JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are given an array of strings, you need to sort the given array of strings containing both uppercase and lowercase characters in ascending order by ignoring the case in JavaScript.

Example 1:

Input :- arr = ["Geeks", "for", "geeks", "is", "The", "Best"] Output :- [ 'Best', 'for', 'Geeks', 'geeks', 'is', 'The' ] Explanation:  In the input array after looking at starting character of each string we find that : B < F < G < I <T. So "Best" will come first followed by "For" then "Geeks" two times then "Is" and finally "The"

Example 2:

Input :- arr = ["Hey", "How", "You", "Doing" ] Output :-  ['Doing', 'Hey', 'How', 'You']

Approaches to sort strings in alphabetical order ignoring case in JavaScript

Table of Content

  • Using a comparator function
  • Without using the comparator function
  • Using Array.sort() with Intl.Collator

Approach 1: Using a comparator function

In this approach, we Initialize the array and use the sort method to sort the array in alphabetical order ignoring case. The sort method then takes a comparator function as its argument. The comparator function compares two elements of the array a and b and then it returns a negative number if a comes before b, a positive number if a comes after b, or 0 if both a and b are equal.

Example: Below is the implementation of this approach

JavaScript
let arr = ["Geeks", "for", "geeks", "is","The","Best"]; arr.sort(function(a, b) {   return a.toLowerCase().localeCompare(b.toLowerCase()); }); console.log(arr); 

Output
[ 'Best', 'for', 'Geeks', 'geeks', 'is', 'The' ] 

Approach 2: Without using the comparator function

Convert the strings to lowercase by using the toLowerCase method, and then compare them using the ternary operator ? . If the lowercase version of string a comes after the lowercase version of string b, then return 1 which means that a should come after b in the sorted array. Otherwise, return -1 meaning that a should come before b in the sorted array.

Example: Below is the implementation of this approach

JavaScript
let arr = ["Geeks", "for", "geeks", "is","The","Best"]; arr.sort((a, b) => a.toLowerCase() > b.toLowerCase() ? 1 : -1); console.log(arr); 

Output
[ 'Best', 'for', 'geeks', 'Geeks', 'is', 'The' ] 

Approach 3: Using Array.sort() with Intl.Collator

Using Array.sort() with Intl.Collator, initialize Collator with { sensitivity: 'accent' } to perform a case-insensitive comparison. Then, sort the array of strings, considering language-specific rules for sorting while ignoring case differences.

Example:

JavaScript
const strings = ["Apple", "banana", "Orange", "grape"];  const collator = new Intl.Collator(undefined, { sensitivity: 'accent' }); strings.sort(collator.compare);  console.log(strings);  

Output
[ 'Apple', 'banana', 'grape', 'Orange' ] 

Next Article
JavaScript Program to Sort Strings in Alphabetical Order Ignoring Case

A

adityajoh8d3r
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-string
  • JavaScript-DSA
  • JavaScript-Program

Similar Reads

    JavaScript Program to Sort Words in Alphabetical Order
    Sorting words in alphabetical order involves arranging them based on the standard sequence of letters in the alphabet. This process helps in organizing data, making searches efficient, and presenting information in a clear and systematic manner.Methods to sort wordsTable of Content Approach 1: Using
    5 min read
    Java Program to Sort Names in an Alphabetical Order
    For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let's use the bubble sort and inbuilt sort. Example: Input : Array[] = {"Sourabh", "Anoop, "Harsh",
    3 min read
    PHP Program to Sort Names in an Alphabetical Order
    Sorting names in alphabetical order is a common task in programming. PHP provides several functions and methods to achieve this. Examples: Input: arr = ["Sourabh", "Anoop, "Harsh", "Alok", "Tanuj"]Output: ["Alok", "Anoop", "Harsh", "Sourabh", "Tanuj"]Input: arr = ["John", "Alice", "Bob", "Eve", "Cha
    2 min read
    How To Sort List Of Strings In Alphabetically
    When working with Python programs, be it any machine learning program or simple text processing program, you might have come across a need to sort a list of strings alphabetically, either in ascending or reverse order. Strings are sorted alphabetically based on their initial letter (a-z or A-Z). But
    3 min read
    JavaScript - Sort a String Alphabetically using a Function
    Here are the various methods to sort a string alphabetically using a function in JavaScript.1. Using split(), sort(), and join() MethodsThis is the most basic and commonly used method to sort a string alphabetically. The string is first converted into an array of characters, sorted, and then joined
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences