JavaScript Sort array of object if value exists sort array object javascript,sort array object javascript by value,

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"]

Answer:1

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);
Answer:2

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)
Answer:3

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 ...

  1. string match function excel
  2. string match function in javascript
  3. string match function in java
  4. string match function in python
  5. string match function in sql
  6. string match function in php
  7. string match function
  8. string match function in c
  9. string match function in r
  10. string match function in jquery
  11. string match function vba
  12. string match function tcl
  13. string match function ruby
  14. js string match function
  15. partial string match function in excel
  16. typescript string match function
  17. lua string match function
  18. tableau string match function
  19. mysql string match function
  20. string-match emacs function

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++) { ...

  1. return first occurrence excel
  2. return first occurrence in sql
  3. grep return first occurrence
  4. regex return first occurrence
  5. pandas return first occurrence
  6. access return only first occurrence
  7. sql return only first occurrence
  8. grep return only first occurrence