chaining sort and filter not working javascriptHow do JavaScript closures work?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I sort a dictionary by value?How do I include a JavaScript file in another JavaScript file?Sort array of objects by string property valueWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?

Is this a crack on the carbon frame?

How can bays and straits be determined in a procedurally generated map?

Why don't electron-positron collisions release infinite energy?

Has the BBC provided arguments for saying Brexit being cancelled is unlikely?

Languages that we cannot (dis)prove to be Context-Free

How much RAM could one put in a typical 80386 setup?

What would happen to a modern skyscraper if it rains micro blackholes?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Problem of parity - Can we draw a closed path made up of 20 line segments...

What defenses are there against being summoned by the Gate spell?

Theorems that impeded progress

How to write a macro that is braces sensitive?

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

Why doesn't H₄O²⁺ exist?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Which models of the Boeing 737 are still in production?

Do I have a twin with permutated remainders?

"You are your self first supporter", a more proper way to say it

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Why does Kotter return in Welcome Back Kotter?

Can I ask the recruiters in my resume to put the reason why I am rejected?

Python: next in for loop

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?



chaining sort and filter not working javascript


How do JavaScript closures work?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I sort a dictionary by value?How do I include a JavaScript file in another JavaScript file?Sort array of objects by string property valueWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















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.










share|improve this question
























  • seems working fine?

    – Anik Islam Abhi
    Mar 22 at 0:11











  • the filter isn't

    – Akza
    Mar 22 at 0:12











  • how come? Please provide some example like what are you expecting and what is showing?

    – Anik Islam Abhi
    Mar 22 at 0:13











  • sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

    – Akza
    Mar 22 at 0:17






  • 1





    If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

    – Garrett Motzner
    Mar 22 at 0:18


















1















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.










share|improve this question
























  • seems working fine?

    – Anik Islam Abhi
    Mar 22 at 0:11











  • the filter isn't

    – Akza
    Mar 22 at 0:12











  • how come? Please provide some example like what are you expecting and what is showing?

    – Anik Islam Abhi
    Mar 22 at 0:13











  • sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

    – Akza
    Mar 22 at 0:17






  • 1





    If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

    – Garrett Motzner
    Mar 22 at 0:18














1












1








1








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.










share|improve this question
















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.






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);





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);






javascript arrays sorting filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 0:31







Akza

















asked Mar 21 at 23:58









AkzaAkza

6818




6818












  • seems working fine?

    – Anik Islam Abhi
    Mar 22 at 0:11











  • the filter isn't

    – Akza
    Mar 22 at 0:12











  • how come? Please provide some example like what are you expecting and what is showing?

    – Anik Islam Abhi
    Mar 22 at 0:13











  • sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

    – Akza
    Mar 22 at 0:17






  • 1





    If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

    – Garrett Motzner
    Mar 22 at 0:18


















  • seems working fine?

    – Anik Islam Abhi
    Mar 22 at 0:11











  • the filter isn't

    – Akza
    Mar 22 at 0:12











  • how come? Please provide some example like what are you expecting and what is showing?

    – Anik Islam Abhi
    Mar 22 at 0:13











  • sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

    – Akza
    Mar 22 at 0:17






  • 1





    If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

    – Garrett Motzner
    Mar 22 at 0:18

















seems working fine?

– Anik Islam Abhi
Mar 22 at 0:11





seems working fine?

– Anik Islam Abhi
Mar 22 at 0:11













the filter isn't

– Akza
Mar 22 at 0:12





the filter isn't

– Akza
Mar 22 at 0:12













how come? Please provide some example like what are you expecting and what is showing?

– Anik Islam Abhi
Mar 22 at 0:13





how come? Please provide some example like what are you expecting and what is showing?

– Anik Islam Abhi
Mar 22 at 0:13













sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

– Akza
Mar 22 at 0:17





sortAndFilter(data, 'name', 'asc', 'sex', 'male', 'string');, i expect the result is only john and steve, and the order is john, then steve since i order it by the name and ascending.

– Akza
Mar 22 at 0:17




1




1





If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

– Garrett Motzner
Mar 22 at 0:18






If you run the snippet, that's what you get. However, sort mutates arrays, and filter does not... So if you check your original argument, it will have been sorted, but not filtered. The return value will be filtered, though.

– Garrett Motzner
Mar 22 at 0:18













1 Answer
1






active

oldest

votes


















0














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






share|improve this answer


















  • 1





    I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

    – Garrett Motzner
    Mar 22 at 16:56











  • @GarrettMotzner noted. Thank you for your advice. I really appreciate it

    – Akza
    Mar 24 at 16:51











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290942%2fchaining-sort-and-filter-not-working-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer


















  • 1





    I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

    – Garrett Motzner
    Mar 22 at 16:56











  • @GarrettMotzner noted. Thank you for your advice. I really appreciate it

    – Akza
    Mar 24 at 16:51















0














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






share|improve this answer


















  • 1





    I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

    – Garrett Motzner
    Mar 22 at 16:56











  • @GarrettMotzner noted. Thank you for your advice. I really appreciate it

    – Akza
    Mar 24 at 16:51













0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 0:34









AkzaAkza

6818




6818







  • 1





    I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

    – Garrett Motzner
    Mar 22 at 16:56











  • @GarrettMotzner noted. Thank you for your advice. I really appreciate it

    – Akza
    Mar 24 at 16:51












  • 1





    I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

    – Garrett Motzner
    Mar 22 at 16:56











  • @GarrettMotzner noted. Thank you for your advice. I really appreciate it

    – Akza
    Mar 24 at 16:51







1




1





I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

– Garrett Motzner
Mar 22 at 16:56





I would say that because you are doing the sort, you probably want to do a shallow clone on the array first, so that you don't end up mutating the array. That will probably save you some bugs in the future.

– Garrett Motzner
Mar 22 at 16:56













@GarrettMotzner noted. Thank you for your advice. I really appreciate it

– Akza
Mar 24 at 16:51





@GarrettMotzner noted. Thank you for your advice. I really appreciate it

– Akza
Mar 24 at 16:51



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290942%2fchaining-sort-and-filter-not-working-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript