-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"
    }
]
DAK
  • 1,395
  • 4
  • 22
  • 35
  • 1
    You are subtracting strings not numbers. Convert to numbers first. – grandouassou Oct 28 '21 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? – jarmod Oct 28 '21 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 – DAK Oct 28 '21 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. – jarmod Oct 28 '21 at 23:27

2 Answers2

3

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);
ProDec
  • 5,390
  • 1
  • 3
  • 12
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)
user3133
  • 592
  • 4
  • 12