-1
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

  1. Second, Come Nodejs Data

  2. 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

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
  • I have already done by using loops as I mentioned in my answer but it is not the good way to solve the problem. So that's why I am posting the same @Rajesh – VIKAS KOHLI Jan 30 '18 at 05:30
  • Also, *Angular/Node/JS* data? Please explain what you mean by that – Rajesh Jan 30 '18 at 05:30
  • @ricky I have mentioned in the question – VIKAS KOHLI Jan 30 '18 at 05:30
  • @Rajesh It means the array sorting take place according to Angularjs then nodejs and so on – VIKAS KOHLI Jan 30 '18 at 05:31
  • @VIKASKOHLI I got it. There are `n` different ways to do it. If you share your current code(* it is not the good way*, hence you are here), it would be easier for us to help you – Rajesh Jan 30 '18 at 05:32
  • 1
    Possible duplicate of [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – AuxTaco Jan 30 '18 at 05:39

1 Answers1

2

You can do it with a single loop:

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++"}];
// create seperate array for angular, node and other
const ang = [];
const node = [];
const js = [];
const other = [];

myArray.forEach(function(v) {
  // push into array based on name
  if (v.name === 'Angularjs')
    ang.push(v)
  else if (v.name === 'Nodejs')
    node.push(v)
  else if (v.name === 'Javascript')
    js.push(v)
  else
    other.push(v);
})

// concat the arrays in the required order
const res = ang.concat(node, js, other);

console.log(res);

UPDATE 1: Using a multidimensional array instead of multiple variable:

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++"}];
// create seperate array for angular, node and other
const temp = [[],[],[],[]]

myArray.forEach(function(v) {
  // push into array based on name
  if (v.name === 'Angularjs')
    temp[0].push(v)
  else if (v.name === 'Nodejs')
    temp[1].push(v)
  else if (v.name === 'Javascript')
    temp[2].push(v)
  else
    temp[3].push(v);
})

// concat the arrays in the required order
const res = [].concat(...temp);

// or [].concat.apply([], temp)

console.log(res);


UPDATE 2: Using an object reference for avoiding if...else:

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++"}];

// create seperate array for angular, node and other
const temp = [
  [],
  [],
  [],
  []
];
const ref = {
  'Angularjs': 0,
  'Nodejs': 1,
  'Javascript': 2
};

myArray.forEach(function(v) {
  temp[v.name in ref ? ref[v.name] : 3].push(v);
})

// concat the arrays in the required order
const res = [].concat(...temp);

// or [].concat.apply([], temp)

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188