user10454516
user10454516

Reputation: 1143

chaining sort and filter not working javascript

I have an array. I want to sort and filter the array. I had try to chain .sort() and .filter(). The .sort() is working good, but not with the .filter(). Here is my example data and function that I had made. Whats goes wrong here?

const data = [{
  name: 'John',
  date: '24 April 2001',
  sex: 'male'
}, {
  name: 'steve',
  date: '12 August 2012',
  sex: 'male'
}, {
  name: 'natasha',
  date: '13 October 1992',
  sex: 'female'
}, {
  name: 'chris',
  date: '8 September 2004',
  sex: 'remain unknown'
}]

sortAndFilter = (arr, orderBy, order, filterBy, filterValue, dataType) => {

  let result;
  if (filterValue === '') {
    if (dataType === 'string') {
      switch (order) {
        case 'asc':
          result = arr.sort((a, b) => a[orderBy].localeCompare(b[orderBy]));
          break;
        case 'dsc':
          result = arr.sort((a, b) => b[orderBy].localeCompare(a[orderBy]));
          break;
        default:
          result = arr.sort((a, b) => a[orderBy].localeCompare(b[orderBy]));
      }
    } else if (dataType === 'date') {
      switch (order) {
        case 'asc':
          result = arr.sort((a, b) => new Date(a[orderBy]) - new Date(b[orderBy]));
          break;
        case 'dsc':
          result = arr.sort((a, b) => new Date(b[orderBy]) - new Date(a[orderBy]));
          break;
        default:
          result = arr.sort((a, b) => new Date(a[orderBy]) - new Date(b[orderBy]));
      }
    }
  } else {
    if (dataType === 'string') {
      switch (order) {
        case 'asc':
          result = arr.sort((a, b) => a[orderBy].localeCompare(b[orderBy])).filter(el => el[filterBy] === filterValue);
          break;
        case 'dsc':
          result = arr.sort((a, b) => b[orderBy].localeCompare(a[orderBy])).filter(el => el[filterBy] === filterValue);
          break;
        default:
          result = arr.sort((a, b) => a[orderBy].localeCompare(b[orderBy])).filter(el => el[filterBy] === filterValue);
      }
    } else if (dataType === 'date') {
      switch (order) {
        case 'asc':
          result = arr.sort((a, b) => new Date(a[orderBy]) - new Date(b[orderBy])).filter(el => el[filterBy] === filterValue);
          break;
        case 'dsc':
          result = arr.sort((a, b) => new Date(b[orderBy]) - new Date(a[orderBy])).filter(el => el[filterBy] === filterValue);
          break;
        default:
          result = arr.sort((a, b) => new Date(a[orderBy]) - new Date(b[orderBy])).filter(el => el[filterBy] === filterValue);
      }
    }
  }

  return result;
}

sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');
console.log('this is not filtered: ', data);

const finalData = sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');

console.log('this one is sorted and filtered: ', finalData);

that is my approach. Whats wrong? or maybe, is there any better approach to achieve this? Thank you in advance.

Upvotes: 2

Views: 4827

Answers (1)

user10454516
user10454516

Reputation: 1143

Thanks to @GarretMotzner and @AnikIslamAbhi for help me in the comment section. Garret already point out whats going wrong. The function is not wrong, but I just have to store the filtered array in a new variable. Because, .filter() did not mutate the array. I've update the questions include the answer above

Upvotes: 1

Related Questions