3

I want to sort an array of objects and looks like this:

  [
        {
            "id": 1,
            "businessEntityId": "BE001",
            "financials": {
                "Npbt": 2323,
                "Interest": 123213,
                "Depreciation": 213123,
                "Ebit": 1312321,
                "EbitDa": 123123,
                "Leasing": 123213
            },
            "type": "Actual",
            "startDate": "2018-06-15T00:00:00.000Z",
            "endDate": "2018-06-15T00:00:00.000Z",
            "model": "Commercial",
            "duration": 12,
            "quality": "Unqualified"
        },
        {
            "id": 2,
            "businessEntityId": "BE002",
            "financials": {
                "Npbt": 2323,
                "Interest": 123213,
                "Depreciation": 213123,
                "Ebit": 1312321,
                "EbitDa": 123123,
                "Leasing": 123213
            },
            "type": "Actual",
            "startDate": "2017-06-15T00:00:00.000Z",
            "endDate": "2017-06-15T00:00:00.000Z",
            "model": "Agribusiness",
            "duration": 12,
            "quality": "Audited"
        },


    ]

I know how do a basic sort for like sort by date but the requirements are a bit tougher. These are the sortingrules in desc. order of priority:

Modeltype: 1.Commercial 2.Agribusiness

If modeltype is Commercial the rules are: 1.Type : Actual and order of quality:Unqualified,Qualified,Unaudited. 2.Type: Projected and order of quality:Projection, Audited.

If modeltype is Aggribusiness the rules are: 1.Type : Historical and order of quality:Unqualified,Qualified,Unaudited. 2.Type: Actual and order of quality:Projection, Audited.

Like I said no straighforward sort but can anyone help me to get on the right path / how would I begin to solve this?

bier hier
  • 20,970
  • 42
  • 97
  • 166
  • What have you tried so far? – Maheer Ali Mar 19 '19 at 07:52
  • possible duplicate of https://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array – AZ_ Mar 19 '19 at 07:54
  • Would you have these in the same array? How would Commercial/Aggribusiness be sorted compared to one another - e.g., Commercial -> Aggribusiness or perhaps they'd be mixed in the list? – VLAZ Mar 19 '19 at 07:55
  • @AZ_ that question is if you have some other array that defines your order but the question doesn't have that. – VLAZ Mar 19 '19 at 07:56
  • order of quality:Unqualified,Qualified,Unaudited.... what does this mean, surely you will have to create an array to follow the order. @VLAZ – AZ_ Mar 19 '19 at 07:59
  • @bier hier Please show input and output with some more items in array. Here with this data we can't test if our solution is working or not. – Maheer Ali Mar 19 '19 at 07:59
  • @Vlaz It is the same array Commercial come before the Agribusiness – bier hier Mar 19 '19 at 07:59

2 Answers2

5

Theoretically, you could use an object for sorting and take type and quality for sorting.

var order = {
        model: { Commercial: 1, Agribusiness: 2 },
        Historical: { Projection: 1, Audited: 2 },
        Actual: { Unqualified: 1, Qualified: 2, Unaudite: 3 },
        BOTTOM: Infinity
    };

array.sort((a, b) =>
    order.model[a.model] - order.model[b.model] ||
    order[a.type][a.quality] - order[b.type][b.quality]
);

For unknow values, you could add a default value (dpending on the position, like -Infinity for a top, or Infinity for a bottom sorting) and use this pattern, like

(order.model[a.model] || order.BOTTOM) - (order.model[b.model] || order.BOTTOM)

For taking this approach, you need to skip a falsy (0) value in the order object.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Array.sort accepts a compare function. You can add your sort logic inside the compare function.

const data = []; // Put data here

const sortedData = data.sort((data1,data2)=>{
  // This function should return 1 if data1 > data2,
  // -1 if data2 > data1
  // And 0 if data1 === data2

  if(data1.model==="Commercial"){
    // Code for commercial
  }else{
    // Code for others
  }
})
jro
  • 900
  • 1
  • 10
  • 21
  • if you will return -> Return 1 if data1 > data2, <- here in your code how the if-else will be called? – AZ_ Mar 19 '19 at 08:03
  • I mean that the function as a whole should return these values, not necessarily at that portion – jro Mar 19 '19 at 08:04
  • @AZ_ what he means is that the function should return the appropriate values, not use the `return` keyword at the point of the comment. Of course you use the return keyword inside your if/else – slebetman Mar 19 '19 at 08:05
  • @AZ_ I have edited the answer to make it clearer. Thanks for pointing out! – jro Mar 19 '19 at 08:06
  • What about sorting in predefined quality order? – bier hier Mar 19 '19 at 08:14