• 0

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.