How to perform case-insensitive sorting array of string in JavaScript?
In (almost :) a one-liner
["Foo", "bar"].sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
Which results in
[ 'bar', 'Foo' ]
While
["Foo", "bar"].sort();
results in
[ 'Foo', 'bar' ]
JavaScript case insensitive sorting for HTML table
This function works to sort strings alphabetically and places uppercase before lowercase.
function (a, b) {
var x = String(a).toLowerCase();
var y = String(b).toLowerCase();
if (x > y)
return -1;
if (x < y)
return 1;
}
Performing a case sensitive string sorting in TypeScript
Try this:
var arr = [{'Id':'1','Value':'Desk'},{'Id':'2','Value':'skool'},{'Id':'3','Value':'OT'},{'Id':'4','Value':'sector'},{'Id':'5','Value':'Security'},{'Id':'6','Value':'Zebra'}];var sort = (a, b) => a > b ? 1 : (a < b ? -1 : 0);var sorted = arr.sort((a,b) => sort(a.Value[0].toLowerCase(), b.Value[0].toLowerCase()) || -sort(a.Value[0], b.Value[0]) || sort(a.Value, b.Value));console.log(sorted);
stop case sensitive sorting array
You can use the String.localeCompare with numeric: true
and caseFirst: "upper"
options for the alpha-numeric. For the symbols case you can match on a regular expression and use charCodeAt
for the sorting:
let data = ['/', '2', '$', '3', 'B', 'a', 'A']let regEx = new RegExp(/[^a-zA-Z\d\s:]/)
let result = data.sort((a, b) => { if (regEx.test(a) && regEx.test(b)) return a.charCodeAt(0) - b.charCodeAt(0) else return a.localeCompare(b, 'en', { numeric: true, caseFirst: 'upper'})})
console.log(result)
Javascript Array sort() does not sort array of strings correctly
It is because when there is no callback function supplied the elements are sorted after converted to UTF-16 code units. In your case it may be the reason that the utf converted string for Kelvin
is before brandy
so it is sorting in that order.
Use localeCompare
const ary = ["Kevin", "brandy", "Andrew"];const nary = ary.sort(function(a, b) { return a.localeCompare(b)
});console.log(nary);
JavaScript's sort method handling of capital letters
That is correct. The strings are being sorted in a binary fashion, using the ordinal values of the characters themselves.
For a case-insensitive sort, try this:
arr.sort(function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
if( a == b) return 0;
return a < b ? -1 : 1;
});
JavaScript case insensitive generic sorting for arrays
_.sortBy
can sort dates - assuming all the values in the column are dates, you only need to test the type and return the date:
const sorted = _.orderBy(this, function(o) {
if ($.isNumeric(o[column])) {
return parseFloat(o[column]);
}
if (_.isDate(o[column])) {
return o[column];
}
return (o[column]).toLowerCase();
}, direction);
javascript filter an array of strings, matching case insensitive substring
this.employeeDisplayList = this.employeeList.filter(e => {
const emp = e.employeeName.toLowerCase();
const str = this.searchStr.toLowerCase();
return emp.includes(str);
});
Related Topics
Using JavaScript to Display a Blob
Three.Js Generate Uv Coordinate
Removing an Anonymous Event Listener
How to Parse JSON to Receive a Date Object in JavaScript
Is There a Built-In Way to Loop Through the Properties of an Object
Check If All Values of Array Are Equal
Vanilla JavaScript Event Delegation
String.Replace' Weird Behavior When Using Dollar Sign ($) as Replacement
How to Remove All Listeners in an Element
What Is This JavaScript Pattern Called and Why Is It Used
Prototype Based VS. Class Based Inheritance
Get Viewport/Window Height in Reactjs
(![]+[])[+[]]... Explain Why This Works