-1

I know similar question is asked before and I referred to this one here: sort json object in javascript but I still cannot find answer to my question. So here I go. I have an JSON object structure as below:

 [
  {
    "toothNumber": "01",
    "name": "John"
  },
  {
    "toothNumber": "18",
    "name": "John"
  },
  {
    "toothNumber": "19",
    "name": "John"
  },
  {
    "toothNumber": "17",
    "name": "John"
  },
  {
    "toothNumber": "01,32",
    "name": "John"
  },
  {
    "toothNumber": "25,32",
    "name": "John"
  },
  {
    "toothNumber": "",
    "name": "John"
  },
  {
    "toothNumber": "15",
    "name": "John"
  }
]

When I use the below code to sort, I do not get expected results:

json.sort(function(a, b){
    return a.toothNumber - b.toothNumber;
});

Below, is the actual result which is not the one I expected. Any help will be appreciated.

Actual Result:

[
  {
    "toothNumber": "",
    "name": "John"
  },
  {
    "toothNumber": "01",
    "name": "John"
  },
  {
    "toothNumber": "15",
    "name": "John"
  },
  {
    "toothNumber": "17",
    "name": "John"
  },
  {
    "toothNumber": "18",
    "name": "John"
  },
  {
    "toothNumber": "19",
    "name": "John"
  },
  {
    "toothNumber": "01,32",
    "name": "John"
  },
  {
    "toothNumber": "25,32",
    "name": "John"
  }
]

Expected Result:

[
    {
        "toothNumber": "",
        "name": "John"
    },
    {
        "toothNumber": "01",
        "name": "John"
    },
    {
        "toothNumber": "01,32",
        "name": "John"
    },
    {
        "toothNumber": "15",
        "name": "John"
    },
    {
        "toothNumber": "17",
        "name": "John"
    },
    {
        "toothNumber": "18",
        "name": "John"
    },
    {
        "toothNumber": "19",
        "name": "John"
    },
    {
        "toothNumber": "25,32",
        "name": "John"
    }
]
4
  • 1
    You are subtracting strings not numbers. Convert to numbers first. Commented Oct 28, 2021 at 22:41
  • Not sure how you expect this to work. The toothnumber is a string first of all, and it's a multi-valued string in certain items. What should be the result of "25,32" - "18", for example? Commented Oct 28, 2021 at 22:41
  • @jarmod those are valid values as saved in the client database. they save multiple tooth numbers comma separated if they are all part of one claim. Do I agree, no but this is the data I have Commented Oct 28, 2021 at 22:52
  • No problem with the representation at all. I'm just highlighting the fact that an implementation that results in "25,32" - "18" makes little sense. Commented Oct 28, 2021 at 23:27

2 Answers 2

4

Sort with string, instead of number comparison.

const json = [  {    "toothNumber": "01",    "name": "John"  },  {    "toothNumber": "18",    "name": "John"  },  {    "toothNumber": "19",    "name": "John"  },  {    "toothNumber": "17",    "name": "John"  },  {    "toothNumber": "01,32",    "name": "John"  },  {    "toothNumber": "25,32",    "name": "John"  },  {    "toothNumber": "",    "name": "John"  },  {    "toothNumber": "15",    "name": "John"  }];

json.sort(function(a, b){
    return a.toothNumber > b.toothNumber ? 1 : (a.toothNumber === b.toothNumber ? 0 : -1 );
});

console.log(json);

Sign up to request clarification or add additional context in comments.

Comments

0

You could just do this

const json = [  {    "toothNumber": "01",    "name": "John"  },  {    "toothNumber": "18",    "name": "John"  },  {    "toothNumber": "19",    "name": "John"  },  {    "toothNumber": "17",    "name": "John"  },  {    "toothNumber": "01,32",    "name": "John"  },  {    "toothNumber": "25,32",    "name": "John"  },  {    "toothNumber": "",    "name": "John"  },  {    "toothNumber": "15",    "name": "John"  }];

json.sort(function(a, b) {
   return a.toothNumber.split(",")[0] - b.toothNumber.split(",")[0];
});

console.log(json)

2 Comments

The parseInt is redundant.
@Spectric yeah it was left when I changed it to work with a snippet. But anyway, split returns string array, so parseInt would be advisable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.