js sort by string code example
Example 1: javascript sort chars in string
const sort = str => str.split('').sort((a, b) => a.localeCompare(b)).join('');
sort('hello world');
Example 2: javascript order by string array
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
Example 3: sort a string in javascript
var string='ACBacb';
var sortedString = string.split('').sort().join('');
Example 4: javascript sort
var names = ["Peter", "Emma", "Jack", "Mia", "Eric"];
names.sort();
var objs = [
{name: "Peter", age: 35},
{name: "Emma", age: 21},
{name: "Jack", age: 53}
];
objs.sort(function(a, b) {
return a.age - b.age;
});
Example 5: javascript sort function
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Example 6: array sort js
arr = ['width', 'score', done', 'neither' ]
arr.sort()
arr.sort((a,b) => a.localeCompare(b))
arr = [21, 7, 5.6, 102, 79]
arr.sort((a, b) => a - b)