0

My json structure as below;

var MyJson = 

[   
    {
        "Country": "Austria",
        "SubCategory": "55",
    }, {
        "Country": "Brazil",
        "SubCategory": "0",
    }, {
        "Country": "Canada",
        "SubCategory": "25",
    }, {
        "Country": "Cyprus",
        "SubCategory": "55",
    },  {
        "Country": "Denmark",
        "SubCategory": "0",
    }, {
        "Country": "France",
        "SubCategory": "25",
    }, {
        "Country": "Greece",
        "SubCategory": "55",
    }, {
        "Country": "Hungary",
        "SubCategory": "0",
    }
];

I am sorting that as below;

_.sortBy(MyJson, 'SubCategory').reverse()

Result as below;

Greece  : 55
Cyprus  : 55
Austria : 55
France  : 25
Canada  : 25
Hungary : 0
Denmark : 0
Brazil  : 0

My expected result as below;

Austria : 55
Cyprus  : 55
Greece  : 55
Canada  : 25
France  : 25
Brazil  : 0
Denmark : 0
Hungary : 0

I have tried as below;

_.sortBy(_.sortBy(MyJson, 'SubCategory').reverse(),'Country');

is there any way to sort json as desc, asc with underscore? I am not using Lodash because of restriction of my development environment.

Thank you in advance.

Kerberos
  • 331
  • 1
  • 11

2 Answers2

5

Plain Javascript approach.

var data = [{ Country: "Austria", SubCategory: "55" }, { Country: "Brazil", SubCategory: "0" }, { Country: "Canada", SubCategory: "25" }, { Country: "Cyprus", SubCategory: "55" }, { Country: "Denmark", SubCategory: "0" }, { Country: "France", SubCategory: "25" }, { Country: "Greece", SubCategory: "55" }, { Country: "Hungary", SubCategory: "0" }];

data.sort(function (a, b) {
    return b.SubCategory - a.SubCategory || a.Country.localeCompare(b.Country);
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Using underscore as stated: you can sort twice: _(MyJson).chain().sortBy('Country').reverse().sortBy('SubCategory').reverse().value();

This answers is what you are looking for.

Here is stated one thing from underscore docs - sortBy is a stable algorithm. That means you can first sort by your second property to put those in the correct order, and then sort by the first property - this will leave two fields with same value for the sorted property in the same order as found. (that order you already set with the first sort)

var MyJson = 

[   
    {
        "Country": "Austria",
        "SubCategory": "55",
    }, {
        "Country": "Brazil",
        "SubCategory": "0",
    }, {
        "Country": "Canada",
        "SubCategory": "25",
    }, {
        "Country": "Cyprus",
        "SubCategory": "55",
    },  {
        "Country": "Denmark",
        "SubCategory": "0",
    }, {
        "Country": "France",
        "SubCategory": "25",
    }, {
        "Country": "Greece",
        "SubCategory": "55",
    }, {
        "Country": "Hungary",
        "SubCategory": "0",
    }
];
var sortedArray =  _(MyJson).chain().sortBy('Country').reverse().sortBy('SubCategory').reverse().value();

console.log(sortedArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
knee pain
  • 600
  • 3
  • 19