Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Filter and sort the given array

Problem description :

Write a function that filters the invalid entries from the given array by removing the following elements :

  • which does not have either id or extension property
  • whose id value is not a number
  • whose extension is not .txt

Sort the filtered array in the ascending order based on the value of id.

Input : An Array
Output : An Array

Logic :

  • Filter the array based on ID, remove the elements which either does not have id or has an invalid ID
  • Filter the array based on extension, remove the elements which either does not have extension or has an invalid extension
  • Sort the filtered array

Solution :

This approach is preferred when re-usability is important.

If performance is important then we can combine both the filter methods into a single filter method.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *