Below is my sort function but I want to display the ones with a code to be at the top. I want to sort my array of objects by code, then by description. Right now, the items without a code is being placed at the top of the order.
data = [
{
code: "1.1",
description: "aaa"
},
{
code: "",
description: "bbb"
},
{
code: "1.2",
description: "ccc"
}
]
let sortedData = data.sort(function(a, b) {
let codeA = a.code
let codeB = b.code
let descriptionA = a.description.toLowerCase()
let descriptionB = b.description.toLowerCase()
if (codeA < codeB) return -1
if (codeA > codeB) return 1
if (descriptionA < descriptionB) return -1
if (descriptionA > descriptionB) return 1
return 0
})
return sortedData
Current order:
["bbb", "aaa", "ccc"]
Expected order:
["aaa", "ccc", "bbb"]
One way to make ""
appear at the end is just to prefix the compare values, so if the code is "", the value becomes "B"
, if it's 1.1
it become A1.1
, so A1.1
< B
..
Doing this is also very easy to modify to do all sort of fancy sorting, eg. Lets say you wanted to sort alphabetically, but wanted all K's
at the start, and all B's
at the end, strange thing to do. But very easy to make happen. I suppose you could call it sorting with exceptions.
Here is a working example.
const data = [
{
code: "1.1",
description: "aaa"
},
{
code: "",
description: "bbb"
},
{
code: "1.2",
description: "ccc"
}
];
data.sort((
{ code: code1, description: desc1 },
{ code: code2, description: desc2 }
) => {
code1 = (code1 === "" ? "B" : "A") + code1;
code2 = (code2 === "" ? "B" : "A") + code2;
return code1.localeCompare(code2) ||
desc1.localeCompare(desc2);
});
console.log(data);
Your test case is a little lax and makes it hard to spot some bugs. You are usually better off using localeCompare
to compare strings. You can make a case for two very plain, but readable if
statements followed by a localeCompare
of the strings:
Here's a few extra test cases
let data = [{
code: "1.1",
description: "ddd"
},
{
code: "1.101",
description: "ccc"
},
{
code: "",
description: "eee"
},
{
code: "1.2",
description: "De"
},
{
code: "1.1",
description: "aaa"
},
{
code: "",
description: "bbb"
},
{
code: "1.2",
description: "ccc"
},
{
code: "1.2",
description: "AbcD"
}
]
data.sort((a, b) => {
if (a.code && !b.code) return -1
if (b.code && !a.code) return 1
return a.code.localeCompare(b.code) || a.description.localeCompare(b.description)
})
console.log(data)
In a swig file I want to see all available properties that it is receiving. Is there a way to access it? Or can I only access its properties? .js res.render('list.swig', result); list.swig {{ ...
In a swig file I want to see all available properties that it is receiving. Is there a way to access it? Or can I only access its properties? .js res.render('list.swig', result); list.swig {{ ...
I am trying to match a regex with some data in a file, the match function however returns null even when the match clearly exists in the data. I have tried same data and regex on RegExr and it shows ...
I am trying to match a regex with some data in a file, the match function however returns null even when the match clearly exists in the data. I have tried same data and regex on RegExr and it shows ...
I saw a code fragment like this: with(document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement(xxx))] I don't know how to understand with(document)0[]
I saw a code fragment like this: with(document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement(xxx))] I don't know how to understand with(document)0[]
I am trying to build logic currently with arrays and data structure. I am trying to implement the logic using for loop function getRepeatingNumber(arr) { for (var i = 0; i < arr.length; i++) { ...
I am trying to build logic currently with arrays and data structure. I am trying to implement the logic using for loop function getRepeatingNumber(arr) { for (var i = 0; i < arr.length; i++) { ...