let myArray = [{"title":"A", "name":"C"}, {"title":"B", "name":"C++"}, {"title":"C", "name":"Java"}, {"title":"D", "name":"Javascript"}, {"title":"E", "name":"Angularjs"}, {"title":"F", "name":"Nodejs"}, {"title":"G", "name":"Express"}, {"title":"H", "name":"HTML"}, {"title":"I", "name":"HTML"}, {"title":"J", "name":"HTML"}, {"title":"K", "name":"Angularjs"}, {"title":"L", "name":"Javascript"}, {"title":"M", "name":"Nodejs"}, {"title":"N", "name":"Nodejs"}, {"title":"O", "name":"C++"}];
Now I want to sort the above array like that:- 1. First, come Angularjs Data
Second, Come Nodejs Data
Third, Come Javascript Data
Then afterwards come according to array order
Required Output will be:-
[{"title":"E", "name":"Angularjs"}, {"title":"K", "name":"Angularjs"}, {"title":"F", "name":"Nodejs"}, {"title":"M", "name":"Nodejs"}, {"title":"N", "name":"Nodejs"}, {"title":"D", "name":"Javascript"},
{"title":"L", "name":"Javascript"}, {"title":"A", "name":"C"}, {"title":"B", "name":"C++"}, {"title":"C", "name":"Java"}, {"title":"H", "name":"HTML"}, {"title":"I", "name":"HTML"}, {"title":"J", "name":"HTML"}]
My solution for doing this:- Loop through the array then after check the required condition, then place the matched value in new array. Similarly, I can get the desired output.
But it will require 4 loops 1 loop for Angularjs, 2nd loop for Nodejs, 3rd loop for Javascript, 4th loop for other values of array and the loop value might increase or decrease depending upon my static values