0

I have an unsorted array of object having 2 revelant property: type, and date. I need to sort this array:

  1. By date and
  2. By type (for every year, deposit should go first before withdrawal)

Here is an example of input:

let i = [
    {
        date:'2017',
        type:'withdrawal',
        amount:-5
    },
    {
        date:'2016',
        type:'deposit',
        amount:12
    },
    {
        date:'2018',
        type:'deposit',
        amount:54
    },
    {
        date:'2017',
        type:'deposit',
        amount:20
    },
    {
        date:'2016',
        type:'withdrawal',
        amount:55
    },
    {
        date:'2018',
        type:'withdrawal',
        amount:54
    }
]  

The goal is to output something like this:

let o = [
    {
        date:'2016',
        type:'deposit',
        amount:12
    },
    {
        date:'2016',
        type:'withdrawal',
        amount:55
    },
    {
        date:'2017',
        type:'deposit',
        amount:20
    },
    {
        date:'2017',
        type:'withdrawal',
        amount:-5
    },
    {
        date:'2018',
        type:'deposit',
        amount:54
    },
    {
        date:'2018',
        type:'withdrawal',
        amount:54
    }
]

So far I managed to sort the array by date using:

o = i.sort((a,b)=>(a.date - b.date))

But I can't find a way to sort it by type

3 Answers 3

3

You can use "sort" like below. By using "localeCompare" it will sort "type" in increasing order - which is "deposit" will come before "withdrawl" as "d" comes before "w"

That is use this -

i.sort((a,b) => ((+a.date) - (+b.date) || (a.type.localeCompare(b.type))))
  • you sort first by "date" and if dates are same then it goes in OR condition (as a.date - b.date will be 0) to check "type"

let i = [
    {
        date:'2017',
        type:'withdrawal',
        amount:-5
    },
    {
        date:'2016',
        type:'deposit',
        amount:12
    },
    {
        date:'2018',
        type:'deposit',
        amount:54
    },
    {
        date:'2017',
        type:'deposit',
        amount:20
    },
    {
        date:'2016',
        type:'withdrawal',
        amount:55
    },
    {
        date:'2018',
        type:'withdrawal',
        amount:54
    }
]  

i.sort((a,b) => ((+a.date) - (+b.date) || (a.type.localeCompare(b.type))))

console.log(i)

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please explain
@TSR I have added "sort" explanation.. Pls let me know if anything is unclear. Thanks.
0

You can use string#localCompare to sort the array first based on date and then on type.

let arr = [ { date:'2017', type:'withdrawal', amount:-5 }, { date:'2016', type:'deposit', amount:12 }, { date:'2018', type:'deposit', amount:54 }, { date:'2017', type:'deposit', amount:20 }, { date:'2016', type:'withdrawal', amount:55 }, { date:'2018',type:'withdrawal', amount:54 } ];
arr.sort((a,b) => a.date.localeCompare(b.date) || a.type.localeCompare(b.type));
console.log(arr);

Comments

0

While probably not the most optimal solution, I concat'ed the type onto the date as a string for both "a" and "b" in the Array.prototype.sort method, and sorted the array based on that.

i.sort((a,b) => {
  let aStr = `${a.date}${a.type}`;
  let bStr = `${b.date}${b.type}`;
  if(aStr > bStr) {
    return 1;
  } else if(aStr === bStr) {
    return 0;
  } else {
    return -1;
  }
});

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.