How to sort a json object by numeric sorting method [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to sort a json object by numeric sorting method [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am having an object with the display value as a string having numbers in it
I want the values to be sorted by Numeric sorting and not alphabetic sorting

Let jsonObj = [{ displayValue : 'B1'}, {displayValue : 'A3'},{ displayValue : 'D1'}, {displayValue : 'C2'}, {displayValue: 'A4'}, {displayValue: 'D3'}];

so the output should have B1, D1, C2, A3, A4

Can any one help me out with the logic for numeric sorting Any help is appreciated

Thanks in Advance

Answer

this will sort the array as you want:

array.sort((a, b) => +a.displayValue.match(/d+/)[0] - +b.displayValue.match(/d+/)[0] > 0 ? 1 : -1);
We are here to answer your question about How to sort a json object by numeric sorting method [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji