Related
I tried to upgrade the current custom sort function of JavaScript to create a new order of sorting e.g. (1, 2, 3, 4,..., !##$%^=+, a, A, b, B, c, C)
function getSortOrder(prop) {
return function (a, b) {
if (isSpecialChar(a[prop], 0) || isSpecialChar(b[prop], 0)) {
return sortData(a[prop], b[prop]);
}
if (isNumeric(a[prop], 0) == "number" || isNumeric(b[prop], 0) == "number") {
return getSortNumeric(a[prop], b[prop]);
}
if (isLetter(a[prop], 0) || isLetter(b[prop], 0)) {
return getSortLetter(a[prop], b[prop]);
}
};
}
function getSortLetter(a, b) {
if ((a.charAt(0) === getLowerCase(a, 0)) && (b.charAt(0) === getUpperCase(b, 0))) {
return sortData(a, b);
}
return sortData(a, b);
}
function getSortNumeric(a, b) {
if (typeof a[prop] == "number") {
return (a[prop] - b[prop]);
} else {
return ((a[prop] < b[prop]) ? -1 : ((a[prop] > b[prop]) ? 1 : 0));
}
}
function sortData(a, b) {
if (a.toLowerCase() < b.toLowerCase()) {
return -1;
} else if (a.toLowerCase() > b.toLowerCase()) {
return 1;
} else {
return 0;
}
}
/**
* Function that is used for the ascending order of number
*
*/
const sortNumberData = (a, b) => a.localeCompare(b, 'en', { numeric: true })
// to check if the data has numeric
function isNumeric(str, index) {
let x = /^[0-9]$/.test(str.charAt(index));
console.log(str, x);
return x;
}
// to determine if the data has neither numeric or letter
function isSpecialChar(str, index) {
return !isNumeric(str, index) && !isLetter(str, index);
}
// to specify the order of letter e.g. (jane doe, Jane Doe, john doe, John doe)
function isLetter(str, index) {
return str.charAt(index).length === 1 && str.match(/[a-z]/i);
}
function getLowerCase(str, index) {
return str.charAt(index).toLowerCase();
}
function getUpperCase(str, index) {
return str.charAt(index).toUpperCase();
}
expected result of Json Values:
List of Users:
123Admin
321user
!testAdmin
#adminData
jane doe
Jane Smith
john doe
John Doe
Current results of Json Values:
List of Users:
!testAdmin
#adminData
123Admin
321user
Jane Smith
jane doe
john doe
It still follows the Ascii default order of sort.
The approach suggested by Nina Scholz is more concise, but here is what was wrong with your original code:
Your isLetter function does not return the correct result. Using the RegExp.test method as below would fix that:
function isLetter(str, index) {
return str.charAt(index).length === 1 && /^[a-z]/i.test(str);
}
Your getSortOrder function also does not handle sorting correctly when comparing characters that belong to different groups (special character / number / letter). To fix that, you could change that function to distinguish when the characters are in the same group versus when they are in different groups:
function getSortOrder(a, b) {
if (isNumeric(a, 0) && isNumeric(b, 0)) return sortData(a, b);
if (isSpecialChar(a, 0) && isSpecialChar(b, 0)) return sortData(a, b);
if (isLetter(a, 0) && isLetter(b, 0)) return sortData(a, b);
if (isNumeric(a, 0)) return -1;
if (isLetter(a, 0)) return 1;
if (isSpecialChar(a, 0)) {
if (isNumeric(b, 0)) return 1;
return -1;
}
}
Finally, the sortData function does not distinguish between lower and upper case. It would need to do something like this:
function sortData(a, b) {
const aLower = a[0].toLowerCase();
const bLower = b[0].toLowerCase();
if (aLower === bLower) {
if (a[0] === aLower && b[0] !== bLower) return -1;
if (a[0] !== aLower && b[0] === bLower) return 1;
return 0;
}
if (aLower < bLower) return -1;
if (aLower > bLower) return 1;
return 0;
}
You could take a brute force approach with an string/object for the wanted order.
This appriach iterate each pair of strings and check any character by getting the order until finding different characters.
const
chars = ' 0123456789!##$%^=+abcdefghijklmnopqrstuvwxyz',
order = Object.fromEntries(Array.from(chars, ((c, i) => [c, i + 1]))),
sort = (a, b) => {
for (let i = 0, l = Math.min(a.length, b.length); i < l; i++) {
const r = order[a[i].toLowerCase()] - order[b[i].toLowerCase()];
if (r) return r;
}
return a.length - b.length;
},
sortBy = (fn, k) => (a, b) => fn(a[k], b[k]),
data = [{ name: 'abcd' }, { name: 'abc' }, { name: 'John Doe' }, { name: '!testAdmin' }, { name: '#adminData' }, { name: '123Admin' }, { name: '321user' }, { name: 'Jane Smith' }, { name: 'jane doe' }, { name: 'john doe' }];
data.sort(sortBy(sort, 'name'));
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here's a function that can be used in a sort.
It starts with finding the index of the first uncommon character between the lower case strings.
Then assigns the order (-1,0,+1) depending on a priority, and then the order of the lower case strings.
function newSort(a, b) {
let lca = a.toLowerCase();
let lcb = b.toLowerCase();
let len = Math.min(a.length, b.length);
let i = 0;
// find index of first uncommon character
while(lca[i] === lcb[i] && i<len) i++;
// what priority do the types of the uncommon character get
let prioA = !lca[i] ? 0 : /^\d/.test(lca[i]) ? 1 : /^[a-z]/.test(lca[i]) ? 3 : 2;
let prioB = !lcb[i] ? 0 : /^\d/.test(lcb[i]) ? 1 : /^[a-z]/.test(lcb[i]) ? 3 : 2;
let order = prioA > prioB ? 1 : prioA < prioB ? -1
: lca > lcb ? 1 : lca < lcb ? -1 : 0;
return order
}
const stringArray = [
"1!a", "1a!", "!1a", "!a1", "a!1", "a1!"
, "Jane Smith" , "jane doe" , "john doe"
, "abcX", "ABC", "DEFy", "defx"
];
let sortedStringArray = stringArray.sort(newSort);
console.log(sortedStringArray);
I have an array of following strings:
['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']
...etc.
I need a solution that will give me following ordered result
['4.5.0', '4.21.0', '4.22.0', '5.1.0', '5.5.1', '6.1.0'].
I tried to implement a sort so it first sorts by the numbers in the first position, than in case of equality, sort by the numbers in the second position (after the first dot), and so on...
I tried using sort() and localeCompare(), but if I have elements '4.5.0' and '4.11.0', I get them sorted as ['4.11.0','4.5.0'], but I need to get ['4.5.0','4.11.0'].
How can I achieve this?
You could prepend all parts to fixed size strings, then sort that, and finally remove the padding again.
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
arr = arr.map( a => a.split('.').map( n => +n+100000 ).join('.') ).sort()
.map( a => a.split('.').map( n => +n-100000 ).join('.') );
console.log(arr)
Obviously you have to choose the size of the number 100000 wisely: it should have at least one more digit than your largest number part will ever have.
With regular expression
The same manipulation can be achieved without having to split & join, when you use the callback argument to the replace method:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
arr = arr.map( a => a.replace(/\d+/g, n => +n+100000 ) ).sort()
.map( a => a.replace(/\d+/g, n => +n-100000 ) );
console.log(arr)
Defining the padding function once only
As both the padding and its reverse functions are so similar, it seemed a nice exercise to use one function f for both, with an extra argument defining the "direction" (1=padding, -1=unpadding). This resulted in this quite obscure, and extreme code. Consider this just for fun, not for real use:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
arr = (f=>f(f(arr,1).sort(),-1)) ((arr,v)=>arr.map(a=>a.replace(/\d+/g,n=>+n+v*100000)));
console.log(arr);
Use the sort compare callback function
You could use the compare function argument of sort to achieve the same:
arr.sort( (a, b) => a.replace(/\d+/g, n => +n+100000 )
.localeCompare(b.replace(/\d+/g, n => +n+100000 )) );
But for larger arrays this will lead to slower performance. This is because the sorting algorithm will often need to compare a certain value several times, each time with a different value from the array. This means that the padding will have to be executed multiple times for the same number. For this reason, it will be faster for larger arrays to first apply the padding in the whole array, then use the standard sort, and then remove the padding again.
But for shorter arrays, this approach might still be the fastest. In that case, the so-called natural sort option -- that can be achieved with the extra arguments of localeCompare -- will be more efficient than the padding method:
var arr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
arr = arr.sort( (a, b) => a.localeCompare(b, undefined, { numeric:true }) );
console.log(arr);
More about the padding and unary plus
To see how the padding works, look at the intermediate result it generates:
[ "100005.100005.100001", "100004.100021.100000", "100004.100022.100000",
"100006.100001.100000", "100005.100001.100000" ]
Concerning the expression +n+100000, note that the first + is the unary plus and is the most efficient way to convert a string-encoded decimal number to its numerical equivalent. The 100000 is added to make the number have a fixed number of digits. Of course, it could just as well be 200000 or 300000. Note that this addition does not change the order the numbers will have when they would be sorted numerically.
The above is just one way to pad a string. See this Q&A for some other alternatives.
If you are looking for a npm package to compare two semver version, https://www.npmjs.com/package/compare-versions is the one.
Then you can sort version like this:
// ES6/TypeScript
import compareVersions from 'compare-versions';
var versions = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
var sorted = versions.sort(compareVersions);
You could split the strings and compare the parts.
function customSort(data, order) {
function isNumber(v) {
return (+v).toString() === v;
}
var sort = {
asc: function (a, b) {
var i = 0,
l = Math.min(a.value.length, b.value.length);
while (i < l && a.value[i] === b.value[i]) {
i++;
}
if (i === l) {
return a.value.length - b.value.length;
}
if (isNumber(a.value[i]) && isNumber(b.value[i])) {
return a.value[i] - b.value[i];
}
return a.value[i].localeCompare(b.value[i]);
},
desc: function (a, b) {
return sort.asc(b, a);
}
}
var mapped = data.map(function (el, i) {
return {
index: i,
value: el.split('')
};
});
mapped.sort(sort[order] || sort.asc);
return mapped.map(function (el) {
return data[el.index];
});
}
var array = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0'];
console.log('sorted array asc', customSort(array));
console.log('sorted array desc ', customSort(array, 'desc'));
console.log('original array ', array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can check in loop if values are different, return difference, else continue
var a=['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
a.sort(function(a,b){
var a1 = a.split('.');
var b1 = b.split('.');
var len = Math.max(a1.length, b1.length);
for(var i = 0; i< len; i++){
var _a = +a1[i] || 0;
var _b = +b1[i] || 0;
if(_a === _b) continue;
else return _a > _b ? 1 : -1
}
return 0;
})
console.log(a)
Though slightly late this would be my solution;
var arr = ["5.1.1","5.1.12","5.1.2","3.7.6","2.11.4","4.8.5","4.8.4","2.10.4"],
sorted = arr.sort((a,b) => {var aa = a.split("."),
ba = b.split(".");
return +aa[0] < +ba[0] ? -1
: aa[0] === ba[0] ? +aa[1] < +ba[1] ? -1
: aa[1] === ba[1] ? +aa[2] < +ba[2] ? -1
: 1
: 1
: 1;
});
console.log(sorted);
Here's a solution I developed based on #trincot's that will sort by semver even if the strings aren't exactly "1.2.3" - they could be i.e. "v1.2.3" or "2.4"
function sortSemVer(arr, reverse = false) {
let semVerArr = arr.map(i => i.replace(/(\d+)/g, m => +m + 100000)).sort(); // +m is just a short way of converting the match to int
if (reverse)
semVerArr = semVerArr.reverse();
return semVerArr.map(i => i.replace(/(\d+)/g, m => +m - 100000))
}
console.log(sortSemVer(["1.0.1", "1.0.9", "1.0.10"]))
console.log(sortSemVer(["v2.1", "v2.0.9", "v2.0.12", "v2.2"], true))
This seems to work provided there are only digits between the dots:
var a = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0']
a = a.map(function (x) {
return x.split('.').map(function (x) {
return parseInt(x)
})
}).sort(function (a, b) {
var i = 0, m = a.length, n = b.length, o, d
o = m < n ? n : m
for (; i < o; ++i) {
d = (a[i] || 0) - (b[i] || 0)
if (d) return d
}
return 0
}).map(function (x) {
return x.join('.')
})
'use strict';
var arr = ['5.1.2', '5.1.1', '5.1.1', '5.1.0', '5.7.2.2'];
Array.prototype.versionSort = function () {
var arr = this;
function isNexVersionBigger (v1, v2) {
var a1 = v1.split('.');
var b2 = v2.split('.');
var len = a1.length > b2.length ? a1.length : b2.length;
for (var k = 0; k < len; k++) {
var a = a1[k] || 0;
var b = b2[k] || 0;
if (a === b) {
continue;
} else
return b < a;
}
}
for (var i = 0; i < arr.length; i++) {
var min_i = i;
for (var j = i + 1; j < arr.length; j++) {
if (isNexVersionBigger(arr[i], arr[j])) {
min_i = j;
}
}
var temp = arr[i];
arr[i] = arr[min_i];
arr[min_i] = temp;
}
return arr;
}
console.log(arr.versionSort());
This solution accounts for version numbers that might not be in the full, 3-part format (for example, if one of the version numbers is just 2 or 2.0 or 0.1, etc).
The custom sort function I wrote is probably mostly what you're looking for, it just needs an array of objects in the format {"major":X, "minor":X, "revision":X}:
var versionArr = ['5.5.1', '4.21.0', '4.22.0', '6.1.0', '5.1.0', '4.5.0'];
var versionObjectArr = [];
var finalVersionArr = [];
/*
split each version number string by the '.' and separate them in an
object by part (major, minor, & revision). If version number is not
already in full, 3-part format, -1 will represent that part of the
version number that didn't exist. Push the object into an array that
can be sorted.
*/
for(var i = 0; i < versionArr.length; i++){
var splitVersionNum = versionArr[i].split('.');
var versionObj = {};
switch(splitVersionNum.length){
case 1:
versionObj = {
"major":parseInt(splitVersionNum[0]),
"minor":-1,
"revision":-1
};
break;
case 2:
versionObj = {
"major":parseInt(splitVersionNum[0]),
"minor":parseInt(splitVersionNum[1]),
"revision":-1
};
break;
case 3:
versionObj = {
"major":parseInt(splitVersionNum[0]),
"minor":parseInt(splitVersionNum[1]),
"revision":parseInt(splitVersionNum[2])
};
}
versionObjectArr.push(versionObj);
}
//sort objects by parts, going from major to minor to revision number.
versionObjectArr.sort(function(a, b){
if(a.major < b.major) return -1;
else if(a.major > b.major) return 1;
else {
if(a.minor < b.minor) return -1;
else if(a.minor > b.minor) return 1;
else {
if(a.revision < b.revision) return -1;
else if(a.revision > b.revision) return 1;
}
}
});
/*
loops through sorted object array to recombine it's version keys to match the original string's value. If any trailing parts of the version
number are less than 0 (i.e. they didn't exist so we replaced them with
-1) then leave that part of the version number string blank.
*/
for(var i = 0; i < versionObjectArr.length; i++){
var versionStr = "";
for(var key in versionObjectArr[i]){
versionStr = versionObjectArr[i].major;
versionStr += (versionObjectArr[i].minor < 0 ? '' : "." + versionObjectArr[i].minor);
versionStr += (versionObjectArr[i].revision < 0 ? '' : "." + versionObjectArr[i].revision);
}
finalVersionArr.push(versionStr);
}
console.log('Original Array: ',versionArr);
console.log('Expected Output: ',['4.5.0', '4.21.0', '4.22.0', '5.1.0', '5.5.1', '6.1.0']);
console.log('Actual Output: ', finalVersionArr);
Inspired from the accepted answer, but ECMA5-compatible, and with regular string padding (see my comments on the answer):
function sortCallback(a, b) {
function padParts(version) {
return version
.split('.')
.map(function (part) {
return '00000000'.substr(0, 8 - part.length) + part;
})
.join('.');
}
a = padParts(a);
b = padParts(b);
return a.localeCompare(b);
}
Usage:
['1.1', '1.0'].sort(sortCallback);
const arr = ["5.1.1","5.1.12","5.1.2","3.7.6","2.11.4","4.8.5","4.8.4","2.10.4"];
const sorted = arr.sort((a,b) => {
const ba = b.split('.');
const d = a.split('.').map((a1,i)=>a1-ba[i]);
return d[0] ? d[0] : d[1] ? d[1] : d[2]
});
console.log(sorted);
This can be in an easier way using the sort method without hardcoding any numbers and in a more generic way.
enter code here
var arr = ['5.1.2', '5.1.1', '5.1.1', '5.1.0', '5.7.2.2'];
splitArray = arr.map(elements => elements.split('.'))
//now lets sort based on the elements on the corresponding index of each array
//mapped.sort(function(a, b) {
// if (a.value > b.value) {
// return 1;
// }
// if (a.value < b.value) {
// return -1;
// }
// return 0;
//});
//here we compare the first element with the first element of the next version number and that is [5.1.2,5.7.2] 5,5 and 1,7 and 2,2 are compared to identify the smaller version...In the end use the join() to get back the version numbers in the proper format.
sortedArray = splitArray.sort((a, b) => {
for (i in a) {
if (parseInt(a[i]) < parseInt(b[i])) {
return -1;
break
}
if (parseInt(a[i]) > parseInt(b[i])) {
return +1;
break
} else {
continue
}
}
}).map(p => p.join('.'))
sortedArray = ["5.1.0", "5.1.1", "5.1.1", "5.1.2", "5.7.2.2"]
sort 1.0a notation correct
use native localeCompare to sort 1.090 notation
function log(label,val){
document.body.append(label,String(val).replace(/,/g," - "),document.createElement("BR"));
}
const sortVersions = (
x,
v = s => s.match(/[a-z]|\d+/g).map(c => c==~~c ? String.fromCharCode(97 + c) : c)
) => x.sort((a, b) => (a + b).match(/[a-z]/)
? v(b) < v(a) ? 1 : -1
: a.localeCompare(b, 0, {numeric: true}))
let v=["1.90.1","1.090","1.0a","1.0.1","1.0.0a","1.0.0b","1.0.0.1","1.0a"];
log(' input : ',v);
log('sorted: ',sortVersions(v));
log('no dups:',[...new Set(sortVersions(v))]);
In ES6 you can go without regex.
const versions = ["0.4", "0.11", "0.4.1", "0.4", "0.4.2", "2.0.1","2", "0.0.1", "0.2.3"];
const splitted = versions.map(version =>
version
.split('.')
.map(i => +i))
.map(i => {
let items;
if (i.length === 1) {
items = [0, 0]
i.push(...items)
}
if (i.length === 2) {
items = [0]
i.push(...items)
}
return i
})
.sort((a, b) => {
for(i in a) {
if (a[i] < b[i]) {
return -1;
}
if (a[i] > b[i]) {
return +1;
}
}
})
.map(item => item.join('.'))
const sorted = [...new Set(splitted)]
If ES6 I do this:
versions.sort((v1, v2) => {
let [, major1, minor1, revision1 = 0] = v1.match(/([0-9]+)\.([0-9]+)(?:\.([0-9]+))?/);
let [, major2, minor2, revision2 = 0] = v2.match(/([0-9]+)\.([0-9]+)(?:\.([0-9]+))?/);
if (major1 != major2) return parseInt(major1) - parseInt(major2);
if (minor1 != minor2) return parseInt(minor1) - parseInt(major2);
return parseInt(revision1) - parseInt(revision2);
});
**Sorted Array Object by dotted version value**
var sampleData = [
{ name: 'Edward', value: '2.1.2' },
{ name: 'Sharpe', value: '2.1.3' },
{ name: 'And', value: '2.2.1' },
{ name: 'The', value: '2.1' },
{ name: 'Magnetic', value: '2.2' },
{ name: 'Zeros', value: '0' },
{ name: 'Zeros', value: '1' }
];
arr = sampleData.map( a => a.value).sort();
var requireData = [];
arr.forEach(function(record, index){
var findRecord = sampleData.find(arr => arr.value === record);
if(findRecord){
requireData.push(findRecord);
}
});
console.log(requireData);
[check on jsfiddle.net][1]
[1]: https://jsfiddle.net/jx3buswq/2/
It is corrected now!!!
Here are examples with simple questions:
Example 1: Find maximum depth of binary tree.
I got the right answer but don't know why my original wrong answer is wrong.
Right answer:
var maxDepth = function(root) {
if (root === null) return 0;
var maxDepth = 1;
maxDepth = maxDepthHelper(root, 1, maxDepth);
return maxDepth;
};
function maxDepthHelper(tree, depth, maxDepth) {
if (tree.left === null && tree.right === null) {
maxDepth = depth > maxDepth ? depth : maxDepth;
return maxDepth;
}
if (tree.left) {
maxDepth = maxDepthHelper(tree.left, depth + 1, maxDepth);
}
if (tree.right) {
maxDepth = maxDepthHelper(tree.right, depth + 1, maxDepth);
}
return maxDepth;
}
Wrong answer:
var maxDepth = function(root) {
if (root === null) return 0;
var maxDepth = 1;
maxDepthHelper(root, 1, maxDepth);
return maxDepth;
};
function maxDepthHelper(tree, depth, maxDepth) {
if (tree.left === null && tree.right === null) {
maxDepth = depth > maxDepth ? depth : maxDepth;
return;
}
if (tree.left) {
maxDepthHelper(tree.left, depth + 1, maxDepth);
}
if (tree.right) {
maxDepthHelper(tree.right, depth + 1, maxDepth);
}
}
It has something to do with me thinking the maxDepth should be changed by the helper function and ultimately when I return that it should return changed but it doesn't. It just returns 1 which is the original thing I assign it. But here in the example below, I am able to change a variable from the parent in the helper function, so what am I missing here?
Example 2: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Solution:
var kthSmallest = function(root, k) {
let smallestArr = [];
kthSmallestHelper(root, k, smallestArr);
return smallestArr.pop()
};
function kthSmallestHelper(bst, k, array) {
if (bst === null) return;
kthSmallestHelper(bst.left, k, array);
if (array.length === k) return;
array.push(bst.val);
kthSmallestHelper(bst.right, k, array);
}
The variable maxDepth (lets call this the outer maxDepth) in the function maxDepth (unfortunate naming) stores a value (the number 1). When you call maxDepthHelper(root, 1, maxDepth), the value 1 is passed in and stored in the local variable maxDepth inside maxDepthHelper. We can assign anything to the local maxDepth, but it won't affect the value stored in the outer maxDepth, because they are two different variables.
The variable smallestArr in the function kthSmallest stores a value; that value is a pointer (the memory location) to an empty array. When kthSmallestHelper(root, k, smallestArr) is called, just like before, that value (the pointer) is passed in and stored inside the local variable array in kthSmallestHelper. Effectively, now array and smallestArr both store a pointer (the memory location) to the same empty array. If we now do any assignment to array, like array=['some new arr'], the variable smallestArr won't get affected. But when you call a mutation method, like array.push(bst), what happens is the Javascript engine looks at the pointer stored in array, and modifies the array stored at that memory location. Because smallestArr stores the pointer to this modified array, if you call smallestArr.pop(), the Javascript engine will pop the last item of the modified array.
The important thing to remember is anytime you write an expression like let x = /* some array or object */, an array/object is created, then a pointer to that array/object is stored in the variable. If you write let x = /* some primitive value (like 3)*/, the value 3 is directly stored in the variable.
In the second program, maxDepth is a number and passed by value (copy), not by reference. The recursive calls are effectively no-ops, and their return values are immediately discarded. This is a common mistake for beginners that are learning how different variable types are passed from one function to another.
That said, recursion is a functional heritage and so using it with functional style yields the best results. This means avoiding side effects like mutation and variable reassignment. You can simplify your depth program a lot -
function depth(tree)
{ if (tree == null)
return 0
else
return 1 + max(depth(tree.left), depth(tree.right))
}
function max (a, b)
{ if (a > b)
return a
else
return b
}
An expression-based syntax is often preferred because expressions evaluate to values, whereas statements (like if and return) do not -
const depth = tree =>
tree == null
? 0
: 1 + max(depth(tree.left), depth(tree.right))
const max = (a, b) =>
a > b
? a
: b
Your kthSmallest program is more difficult but JavaScript's imperative-style generators make quick work of the problem. Mutation k-- is used but cannot be observed from outside of the function -
function *inorder (tree)
{ if (tree == null) return
yield* inorder(tree.left)
yield tree.val
yield* inorder(tree.right)
}
function kthSmallest (tree, k)
{ for (const v of inorder(tree))
if (k-- == 0)
return v
}
The pure expression form of this program is slightly different -
const inorder = tree =>
tree == null
? []
: [ ...inorder(tree.left), tree.val, ...inorder(tree.right) ]
const kthSmallest = (tree, k) =>
inorder(tree)[k]
Here's a functioning demonstration -
import { depth, fromArray, inorder, kthSmallest } from "./Tree"
const rand = _ =>
Math.random() * 100 >> 0
const t =
fromArray(Array.from(Array(10), rand))
console.log("inorder:", Array.from(inorder(t)))
console.log("depth:", depth(t))
console.log("0th:", kthSmallest(t, 0))
console.log("1st:", kthSmallest(t, 1))
console.log("2nd:", kthSmallest(t, 2))
console.log("99th:", kthSmallest(t, 99))
Output -
inorder: [ 12, 14, 25, 44, 47, 53, 67, 70, 85, 91 ]
depth: 5
0th: 12
1st: 14
2nd: 25
99th: undefined
Writing modules like Tree below is a good practice for separating concerns and organising your code -
// Tree.js
const empty =
null
const node = (val, left = empty, right = empty) =>
({ val, left, right })
const fromArray = (a = []) =>
a.length < 1
? empty
: insert(fromArray(a.slice(1)), a[0])
const insert = (t = empty, v = null) =>
t === empty
? node(v)
: v < t.val
? node(t.val, insert(t.left, v), t.right)
: v > t.val
? node(t.val, t.left, insert(t.right, v))
: t
const depth = (tree = empty) => ...
const inorder = (tree = empty) => ...
const kthSmallest = (tree = empty, k = 0) => ...
export { depth, empty, fromArray, inorder, kthSmallest, node }
Expand the snippet below to verify the results in your own browser -
const empty =
null
const node = (val, left = empty, right = empty) =>
({ val, left, right })
const fromArray = (a = []) =>
a.length < 1
? empty
: insert(fromArray(a.slice(1)), a[0])
const insert = (t = empty, v) =>
t === empty
? node(v)
: v < t.val
? node(t.val, insert(t.left, v), t.right)
: v > t.val
? node(t.val, t.left, insert(t.right, v))
: t
const inorder = (tree = empty) =>
tree === empty
? []
: [ ...inorder(tree.left), tree.val, ...inorder(tree.right) ]
const kthSmallest = (tree = empty, k = 0) =>
inorder(tree)[k]
const depth = (tree = empty) =>
tree == null
? 0
: 1 + Math.max(depth(tree.left), depth(tree.right))
const rand = _ =>
Math.random() * 100 >> 0
const t =
fromArray(Array.from(Array(10), rand))
console.log("inorder:", JSON.stringify(Array.from(inorder(t))))
console.log("depth:", depth(t))
console.log("0th:", kthSmallest(t, 0))
console.log("1st:", kthSmallest(t, 1))
console.log("2nd:", kthSmallest(t, 2))
console.log("99th:", kthSmallest(t, 99))
I want to sort the below array by the "name" that is inside "user" object
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}}
]
how can I do this?
I have a method to sort array of objects but I can't use it for array of objects of objects
this is the method:
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
then I can sort using this:
myArr.sort(dynamicSort("id"));
I would create property as a getter function (For the complex examples. You could check if propFn is a function, use this below for the more complex ones. See this answer for checking is propFn is a function.):
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}}
]
function dynamicSort(propFn, sortOrder = 1) {
return function (a, b) {
var result = (propFn(a) < propFn(b)) ? -1 : (propFn(a) > propFn(b)) ? 1 : 0;
return result * sortOrder;
}
}
console.log(myArr.sort(dynamicSort((obj) => obj.user.name)));
console.log(myArr.sort(dynamicSort((obj) => obj.user.name, -1)));
Alternatively, you can take a look at: Convert JavaScript string in dot notation into an object reference
This will give you an idea of how you can convert period notation into a nested object, but I recommend reading the disclaimer at the top.
To maintain backwards compatibility, you could use something like this below:
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}}
]
function dynamicSort(propFn, sortOrder = 1) {
if (typeof propFn === "string") {
let prop = propFn;
if (prop[0] === "-") {
sortOrder = -1;
prop = prop.substr(1);
}
propFn = (obj) => obj[prop];
}
return function (a, b) {
var result = (propFn(a) < propFn(b)) ? -1 : (propFn(a) > propFn(b)) ? 1 : 0;
return result * sortOrder;
}
}
console.log(myArr.sort(dynamicSort((obj) => obj.user.name)));
console.log(myArr.sort(dynamicSort((obj) => obj.user.name, -1)));
console.log(myArr.sort(dynamicSort("id")));
console.log(myArr.sort(dynamicSort("-id")));
Edit:
If you are experiencing problems because of periods in your key names, this approach may be better suited as a solution. The path just has to either start as a bracket notation accessor or with a dot:
function dynamicSort(property, order) {
order||(order=1);
const getter = new Function("obj", "return obj" + property + ";");
return function(a, b) {
var result = (getter(a) < getter(b)) ? -1 : (getter(a) > getter(b)) ? 1 : 0;
return result * order;
}
}
var myArr = [{
"id": 1,
"user": {
"name": "allen",
"id": 101
}
},
{
"id": 2,
"user": {
"name": "martin",
"id": 102
}
},
{
"id": 3,
"user": {
"name": "barry",
"id": 103
}
}
]
console.log(JSON.stringify(myArr.sort(dynamicSort(".user.name"))));
Using the Object.byString() method from this answer, you can rewrite your function to take a path to the property you want to sort by:
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}},
{"id":3,"user":{"name":"barry","id":103}}
]
console.log(JSON.stringify(myArr.sort(dynamicSort("user.name"))));
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (byString(a, property) < byString(b, property)) ? -1 : (byString(a, property) > byString(b, property)) ? 1 : 0;
return result * sortOrder;
}
}
function byString(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
I think it would be a little clearer and easier to use if you have the order as a second parameter, which means that your function should then more or less look like this:
function dynamicSort(property, order) {
return function(a, b) {
var result = (byString(a, property) < byString(b, property)) ? -1 : (byString(a, property) > byString(b, property)) ? 1 : 0;
return result * order;
}
}
You could use sort method but you first need to get nested property and for that you could pass a string and then use reduce method to get property.
var myArr = [{"id":2,"user":{"name":"martin","id":102}}, {"id":1,"user":{"name":"allen","id":101}}]
function dynamicSort(arr, prop) {
function getVal(obj, prop) {
return prop.split('.').reduce((r, e) => r[e] || {}, obj)
}
arr.sort((a, b) => {
let vA = getVal(a, prop);
let vB = getVal(b, prop);
return vA.localeCompare(vB)
})
}
dynamicSort(myArr, "user.name")
console.log(myArr)
Check out this SO answer for an answer to a fundamentally similar question.
Since the function in that answer is called differently than yours, with the array to be sorted passed in as a parameter, you could refactor it to be called in the same way as your existing dynamicSort function as follows:
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
var prop = property.split('.');
var len = prop.length;
return function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
var result = (a < b) ? -1 : (a > b) ? 1 : 0;
return result * sortOrder;
}
}
You could then call it like so: myArr.sort(this.dynamicSort("user.name")).
Here is a working snippet to demonstrate:
function dynamicSort(property) {
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
var prop = property.split('.');
var len = prop.length;
return function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
var result = (a < b) ? -1 : (a > b) ? 1 : 0;
return result * sortOrder;
}
}
var myArr = [
{"id":1,"user":{"name":"allen","id":101}},
{"id":2,"user":{"name":"martin","id":102}},
{"id":3,"user":{"name":"beth","id":103}},
];
console.log(myArr.sort(this.dynamicSort("user.name"))); //expected output: [{id:1, user:{name:"allen",...}}, {id:3, user:{name:"beth",...}}, {id:2, user:{name:"martin",...}}]
Try this out. Works fine for me
var sortedArray = myArr.sort((a, b) => {
const
nameA = a.user.name.toUpperCase(),
nameB = b.user.name.toUpperCase();
if(nameA < nameB)
return -1;
if(nameA > nameB)
return 1;
return 0;
});
myArr.sort(function(a, b) {
return a.user.name.localeCompare(b.user.name);
});
I have this function to sort a JavaScript array of objects based on a property:
// arr is the array of objects, prop is the property to sort by
var sort = function (prop, arr) {
arr.sort(function (a, b) {
if (a[prop] < b[prop]) {
return -1;
} else if (a[prop] > b[prop]) {
return 1;
} else {
return 0;
}
});
};
It works with arrays like this:
sort('property', [
{property:'1'},
{property:'3'},
{property:'2'},
{property:'4'},
]);
But I want to be able to sort also by nested properties, for example something like:
sort('nestedobj.property', [
{nestedobj:{property:'1'}},
{nestedobj:{property:'3'}},
{nestedobj:{property:'2'}},
{nestedobj:{property:'4'}}
]);
However this doesn't work because it is not possible to do something like object['nestedobj.property'], it should be object['nestedobj']['property'].
Do you know how could I solve this problem and make my function work with properties of nested objects?
Thanks in advance
You can split the prop on ., and iterate over the Array updating the a and b with the next nested property during each iteration.
Example: http://jsfiddle.net/x8KD6/1/
var sort = function (prop, arr) {
prop = prop.split('.');
var len = prop.length;
arr.sort(function (a, b) {
var i = 0;
while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return arr;
};
Use Array.prototype.sort() with a custom compare function to do the descending sort first:
champions.sort(function(a, b) { return b.level - a.level }).slice(...
Even nicer with ES6:
champions.sort((a, b) => b.level - a.level).slice(...
Instead of passing the property as a string, pass a function that can retrieve the property from the top level object.
var sort = function (propertyRetriever, arr) {
arr.sort(function (a, b) {
var valueA = propertyRetriever(a);
var valueB = propertyRetriever(b);
if (valueA < valueB) {
return -1;
} else if (valueA > valueB) {
return 1;
} else {
return 0;
}
});
};
Invoke as,
var simplePropertyRetriever = function(obj) {
return obj.property;
};
sort(simplePropertyRetriever, { .. });
Or using a nested object,
var nestedPropertyRetriever = function(obj) {
return obj.nestedObj.property;
};
sort(nestedPropertyRetriever, { .. });
if you have array of objects like
const objs = [{
first_nom: 'Lazslo',
last_nom: 'Jamf',
moreDetails: {
age: 20
}
}, {
first_nom: 'Pig',
last_nom: 'Bodine',
moreDetails: {
age: 21
}
}, {
first_nom: 'Pirate',
last_nom: 'Prentice',
moreDetails: {
age: 22
}
}];
you can use simply
nestedSort = (prop1, prop2 = null, direction = 'asc') => (e1, e2) => {
const a = prop2 ? e1[prop1][prop2] : e1[prop1],
b = prop2 ? e2[prop1][prop2] : e2[prop1],
sortOrder = direction === "asc" ? 1 : -1
return (a < b) ? -sortOrder : (a > b) ? sortOrder : 0;
}
and call it
for direct objects
objs.sort(nestedSort("last_nom"));
objs.sort(nestedSort("last_nom", null, "desc"));
for nested objects
objs.sort(nestedSort("moreDetails", "age"));
objs.sort(nestedSort("moreDetails", "age", "desc"));
You can use Agile.js for this kind of things.
Actually you pass an expression instead of callback, it's handle nested properties and javascript expression in a very nice-ish way.
Usage: _.orderBy(array, expression/callback, reverse[optional])
Example:
var orders = [
{ product: { price: 91.12, id: 1 }, date: new Date('01/01/2014') },
{ product: { price: 79.21, id: 2 }, date: new Date('01/01/2014') },
{ product: { price: 99.90, id: 3 }, date: new Date('01/01/2013') },
{ product: { price: 19.99, id: 4 }, date: new Date('01/01/1970') }
];
_.orderBy(orders, 'product.price');
// → [orders[3], orders[1], orders[0], orders[2]]
_.orderBy(orders, '-product.price');
// → [orders[2], orders[0], orders[1], orders[3]]
Would this meet your needs?
// arr is the array of objects, prop is the property to sort by
var sort = function (nestedObj, prop, arr) {
arr.sort(function (a, b) {
if (a[nestedObj][prop] < b[nestedObj][prop]) {
return -1;
} else if (a[nestedObj][prop] > b[nestedObj][prop]) {
return 1;
} else {
return 0;
}
});
};
Try this (used a recursive function to get nested value, you can pass the nested property as nestedobj.property):
You can use this for any level of hierarchy
// arr is the array of objects, prop is the property to sort by
var getProperty = function(obj, propNested){
if(!obj || !propNested){
return null;
}
else if(propNested.length == 1) {
var key = propNested[0];
return obj[key];
}
else {
var newObj = propNested.shift();
return getProperty(obj[newObj], propNested);
}
};
var sort = function (prop, arr) {
arr.sort(function (a, b) {
var aProp = getProperty(a, prop.split("."));
var bProp = getProperty(a, prop.split("."));
if (aProp < bProp) {
return -1;
} else if (aProp > bProp) {
return 1;
} else {
return 0;
}
});
};
This is my modify code.
// arr is the array of objects, prop is the property to sort by
var s = function (prop, arr) {
// add sub function for get value from obj (1/2)
var _getVal = function(o, key){
var v = o;
var k = key.split(".");
for(var i in k){
v = v[k[i]];
}
return v;
}
return arr.sort(function (a, b) {
// get value from obj a, b before sort (2/2)
var aVal = _getVal(a, prop);
var bVal = _getVal(b, prop);
if (aVal < bVal) {
return -1;
} else if (aVal > bVal) {
return 1;
} else {
return 0;
}
});
};
var objectsArr = [
{nestedobj:{property:'1'}},
{nestedobj:{property:'3'}},
{nestedobj:{property:'2'}},
{nestedobj:{property:'4'}}
];
function getFromPath(obj, path) {
let r = obj;
path.forEach(key => { r = r[key]})
return r
}
function sortObjectsArr(objectsArray, ...path) {
objectsArray.sort((a, b) => getFromPath(a, path) - getFromPath(b, path))
}
sortObjectsArr(objectsArr, 'nestedobj', 'property');
console.log(objectsArr);
Unfortunately, I didn't find any nice way to use the arguments in order to access the attributes of the nested object.
Want to mention that there can be some checks if the keys are available in the passed object, but this depends on who and how want to implement this.
Description
My solution is this one. I decide to flat the object first:
function flattenObject(value: any): any {
let toReturn: any = {};
for (const i in value) {
if (!value.hasOwnProperty(i)) {
continue;
}
if (typeof value[i] == 'object') {
const flatObject = flattenObject(value[i]);
for (const x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = value[i];
}
}
return toReturn;
}
And then I'll extract the value from the object:
function nestedFieldValue(
nestedJoinedFieldByDot: string,
obj: any,
): any {
return flattenObject(obj)[nestedJoinedFieldByDot];
}
Ant at the end I just need to do this:
export function fieldSorter(fields: string[]) {
return function (a: any, b: any) {
return fields
.map(function (fieldKey) {
// README: Sort Ascending by default
let dir = 1;
if (fieldKey[0] === '-') {
// README: Sort Descending if `-` was passed at the beginning of the field name
dir = -1;
fieldKey = fieldKey.substring(1);
}
const aValue = nestedFlattenObjectFieldValue(
fieldKey,
a,
);
const bValue = nestedFlattenObjectFieldValue(
fieldKey,
b,
);
if (
typeof aValue === 'number' ||
typeof bValue === 'number'
) {
/**
* README: default value when the field does not exists to prevent unsorted array
* I assume that 0 should be the last element. In other word I sort arrays in a way
* that biggest numbers comes first and then smallest numbers
*/
if (aValue ?? 0 > bValue ?? 0) {
return dir;
}
if (aValue ?? 0 < bValue ?? 0) {
return -dir;
}
} else {
if (aValue ?? 0 > bValue ?? 0) {
return dir;
}
if (aValue ?? 0 < bValue ?? 0) {
return -dir;
}
}
return 0;
})
.reduce(function firstNonZeroValue(p, n) {
return p ? p : n;
}, 0);
};
}
Finally we need to do this:
const unsorted = [
{
city: {
priority: 1,
name: 'Tokyo',
airport: { name: 'Haneda Airport' }
}
}
]
const result = unsorted.sort(
fieldSorter(['city.priority', 'city.airport.name', 'city.name']),
);
I think this way is much much clear and cleaner. It is readable and more functional. I merge multiple answer from stackoverflow to reach this solution :sweat_smile:
This should work and can accept multiple parameters.
https://codepen.io/allenACR/pen/VwQKWZG
function sortArrayOfObjects(items, getter) {
const copy = JSON.parse(JSON.stringify(items));
const sortFn = fn => {
copy.sort((a, b) => {
a = fn(a)
b = fn(b)
return a === b ? 0 : a < b ? -1 : 1;
});
};
getter.forEach(x => {
const fn = typeof x === 'function' ? x : item => item[x];
sortFn(fn);
});
return copy;
}
// example dataset
const data = [
{id: 3, name: "Dave", details: {skill: "leader"} },
{id: 1, name: "Razor", details: {skill: "music"} },
{id: 2, name: "Syd", details: {skill: "animal husbandry"} }
]
// sort via single prop
const sort1 = sortArrayOfObjects(data, ["id"])
// returns [Razor, Syd, Dave]
// sort via nested
const sort2 = sortArrayOfObjects(data, [
(item) => item.details.skill
])
// returns [Syd, Dave, Razor]
console.log({sort1, sort2})
3 levels deep. path can look like this.
'level1' or 'level1.level2' or 'level1.level2.level3'
I also did uppercase for the sort all my items are strings.
Anwser is a modified version from - #Mas
public keysrt(arr: Object[], path: string, reverse: boolean): void {
const nextOrder = reverse ? -1 : 1;
const pathSplit = path.split('.');
if (arr === null || arr === undefined ) {
return;
}
if (arr.length <= 1) {
return;
}
const nestedSort = (prop1, prop2 = null, prop3 = null, direction = 'asc') => (e1, e2) => {
const a = prop3 ? e1[prop1][prop2][prop3] : prop2 ? e1[prop1][prop2] : e1[prop1],
b = prop3 ? e2[prop1][prop2][prop3] : prop2 ? e2[prop1][prop2] : e2[prop1],
sortOrder = direction === 'asc' ? 1 : -1;
return (a.toString().toUpperCase() < b.toString().toUpperCase()) ?
-sortOrder * nextOrder : (a.toString().toUpperCase() > b.toString().toUpperCase()) ?
sortOrder * nextOrder : 0;
};
if (pathSplit.length === 3) {
arr.sort(nestedSort(pathSplit[0], pathSplit[1], pathSplit[2]));
}
if (pathSplit.length === 2) {
arr.sort(nestedSort(pathSplit[0], pathSplit[1]));
}
if (pathSplit.length === 1) {
arr.sort(nestedSort(pathSplit[0], null));
}
}
For those who a researching on how to sort nested properties. I created a small type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.
https://github.com/jvandenaardweg/sort-by-property
https://www.npmjs.com/package/sort-by-property
Example:
blogPosts.sort(sortByProperty('author.name', 'asc'));