Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Case-sensitive sort in JavaScript
JavaScript's default sort() method performs case-sensitive sorting but places uppercase letters before lowercase letters. This article demonstrates how to implement true case-sensitive sorting where special characters and numbers come first, followed by lowercase letters, then uppercase letters.
JavaScript Case Sensitivity
JavaScript is case-sensitive, meaning it treats uppercase and lowercase letters as completely different characters.
const language = "JavaScript"; const Language = "React"; const x = 100; console.log(language); console.log(Language); console.log(x);
JavaScript React 100
Default Sort Behavior
JavaScript's sort() method converts elements to strings and sorts by UTF-16 character codes. By default, it places uppercase letters before lowercase letters:
const fruits = ["Banana", "apple", "Kiwi", "grapes", "Ice-cream"]; console.log(fruits.sort());
['Banana', 'Ice-cream', 'Kiwi', 'apple', 'grapes']
Numbers and special characters are sorted before letters:
const mixed = ["Banana", "1", "apple", "$special"]; console.log(mixed.sort());
['$special', '1', 'Banana', 'apple']
Using localeCompare for Custom Sorting
The localeCompare() method compares strings and returns:
- Negative value (-1): Reference string comes before comparison string
- Positive value (1): Reference string comes after comparison string
- Zero (0): Strings are equal
function basicSort(a, b) {
return a.localeCompare(b);
}
const mixed = ["Banana", "1", "apple"];
console.log(mixed.sort(basicSort));
['1', 'apple', 'Banana']
Custom Case-Sensitive Sort Implementation
Here's a custom sorting function that prioritizes special characters and numbers first, then lowercase letters, then uppercase letters:
function caseSensitiveSort(a, b) {
// If strings are identical
if (a === b) return 0;
// Compare character by character
for (let i = 0; i < Math.min(a.length, b.length); i++) {
const charA = a.charAt(i);
const charB = b.charAt(i);
if (charA === charB) continue;
// Check if characters are the same letter (different case)
if (charA.toLowerCase() === charB.toLowerCase()) {
// Lowercase comes before uppercase
if (/[a-z]/.test(charA) && /[A-Z]/.test(charB)) return -1;
if (/[A-Z]/.test(charA) && /[a-z]/.test(charB)) return 1;
}
// Use localeCompare for different characters
return charA.localeCompare(charB);
}
// If one string is prefix of another, shorter comes first
return a.length - b.length;
}
const testArray = ["123", "Hello", "apple", "$special", "JAVASCRIPT", "hello"];
console.log(testArray.sort(caseSensitiveSort));
['$special', '123', 'apple', 'hello', 'Hello', 'JAVASCRIPT']
Comparison of Sorting Methods
| Method | Order | Example Result |
|---|---|---|
| Default sort() | Special chars ? Numbers ? Uppercase ? Lowercase | ['$', '1', 'A', 'a'] |
| localeCompare() | Case-insensitive alphabetical | ['$', '1', 'a', 'A'] |
| Custom case-sensitive | Special chars ? Numbers ? Lowercase ? Uppercase | ['$', '1', 'a', 'A'] |
Performance Considerations
The custom sorting algorithm has O(n log n) time complexity due to the underlying sort method, with additional O(m) character comparison where m is the average string length. While not the most optimized solution, it provides the required case-sensitive behavior.
Conclusion
Custom case-sensitive sorting in JavaScript requires implementing a comparison function that handles character precedence rules. The localeCompare() method combined with character type checking provides an effective solution for sorting with lowercase letters prioritized over uppercase letters.
