-2

I am trying to order the below object by name. I want the list to be ordered in the following way 1)balloon 2)term 3)instalment.

loanAdjustmentList = [
    {
        "description": "Restructure Option",
        "name": "instalment",
        "restructureAllowed": "N",
        "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
        "reasonCode": "1"
    },
    {
        "description": "Restructure Option",
        "name": "term",
        "restructureAllowed": "N",
        "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
        "reasonCode": "1"
    },
    {
        "description": "Restructure Option",
        "name": "balloon",
        "restructureAllowed": "N",
        "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
        "reasonCode": "1"
    }
]

Any idea what I can try?

skydev
  • 1,867
  • 9
  • 37
  • 71
  • This is a simple reverse. `this.loanAdjustmentList.reverse();` – Emilien Jul 20 '21 at 08:29
  • Look at the docs of [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) - you can also supply a function, so you can string-compare with each element's `.name`. – Peter Krebs Jul 20 '21 at 08:30
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Gangula Jul 20 '21 at 08:31

2 Answers2

2

You are looking to sort the array.

You just have to write a sort function that utilizes another object as a map.

    const order = ["balloon", "installment", "term"]
    
    loanAdjustmentList.sort((el1, el2) => { 
    if (order.indexOf(el1.name) < order.indexOf(el2.name) {
    return -1;
    } else (order.indexOf(el1.name) > order.indexOf(el2.name) {
    return 1;
    } else {
return 0;
}
    });

This is of course assuming that they will only be one of the three options in question. If you have any other name type, it may throw an error.

I have included the possibility that the name element could be the same, which would allow for sorting of more than just 3 options.

CVerica
  • 327
  • 1
  • 10
  • Max options will be 3, no duplicate names. But I can also have only one or two options that come back. – skydev Jul 20 '21 at 09:08
1

If you meant sorting by the three mentioned names, you can try this:

let loanAdjustmentList = [
        {
            "description": "Restructure Option",
            "name": "instalment",
            "restructureAllowed": "N",
            "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
            "reasonCode": "1"
        },
        {
            "description": "Restructure Option",
            "name": "term",
            "restructureAllowed": "N",
            "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
            "reasonCode": "1"
        },
        {
            "description": "Restructure Option",
            "name": "balloon",
            "restructureAllowed": "N",
            "reason": "Due to the status of your account we are unable to process your request. Please contact 0860 669 669 for more information",
            "reasonCode": "1"
        }
    ]

    loanAdjustmentList = [
        ...loanAdjustmentList.filter(x => x.name === 'balloon'),
        ...loanAdjustmentList.filter(x => x.name === 'term'),
        ...loanAdjustmentList.filter(x => x.name === 'instalment')
        ];
    console.log(loanAdjustmentList);
CyberDev
  • 228
  • 1
  • 7
  • 1
    check @CVerica answer. That's more affective than this. – CyberDev Jul 20 '21 at 08:41
  • Apparently they just wanted the first answer. Thanks for the love though. ¯\_(ツ)_/¯ – CVerica Jul 20 '21 at 09:02
  • 1
    @CVerica I can also get a list that doesnt have have all the options. it can sometimes have only 1 or 2 options come back. sorry I should have been clearer and will also test your solution and change answer if need be :) – skydev Jul 20 '21 at 09:05
  • No worries! I'm not upset. I hope I was helpful. Both of these two options should do the trick. – CVerica Jul 20 '21 at 18:49