-1

I have Array in the name of students in that array I have four different objects. Now I have to print those objects, but the age should be in ascending order and I have to do this only by using forEach only.

var students = [{
    name: 'Mark',
    age: 28
  },
  {
    name: 'Johnson',
    age: 30
  },
  {
    name: 'Williams',
    age: 12
  },
  {
    name: 'Henry',
    age: 27
  }
]

students.forEach(fun)

function fun(currentValue, index, arr) {
  console.log(currentValue)
}
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
Vamsi
  • 151
  • 1
  • 4
  • 11

5 Answers5

2

You don't use forEach to sort an array, you use sort (unsurprisingly!)

var students = [{
    name: 'Mark',
    age: 28
  },
  {
    name: 'Johnson',
    age: 30
  },
  {
    name: 'Williams',
    age: 12
  },
  {
    name: 'Henry',
    age: 27
  }
]

students.sort(sortByAge).forEach(fun)

function sortByAge(a,b){
    return a.age - b.age;
}

function fun(currentValue, index, arr) {
  console.log(currentValue)
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

I think you have to sort the arrya and print it.

Hope this works.

var students = [{
    name: 'Mark',
    age: 28
  },
  {
    name: 'Johnson',
    age: 30
  },
  {
    name: 'Williams',
    age: 12
  },
  {
    name: 'Henry',
    age: 27
  }
]

function sortArray( a, b ) {
  if ( a.age < b.age ){
    return -1;
  }
  if ( a.age > b.age ){
    return 1;
  }
  return 0;
}

students.sort( sortArray );

students.forEach(fun)

function fun(currentValue, index, arr) {
  console.log(currentValue)
}
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0

I hope this is a dynamic solution which is used your system in future as well.

function dynamicSort(property) {
    var sortOrder = 1;
    if(property[0] === "-") {
        sortOrder = -1;
        property = property.substr(1);
    }
    return function (a,b) {
        /* next line works with strings and numbers, 
         * and you may want to customize it to your needs
         */
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
        return result * sortOrder;
    }
}

and it will work when you do:

students.sort(dynamicSort("name"));
students.sort(dynamicSort("name"));
Ravi Chauhan
  • 1,409
  • 13
  • 26
0

If I'm understanding the problem correctly, you might be allowed to do a simple bubble sort.

Assuming you had something like this

var students = [{
    name: 'Mark',
    age: 28
  },
  {
    name: 'Johnson',
    age: 30
  },
  {
    name: 'Williams',
    age: 12
  },
  {
    name: 'Henry',
    age: 27
  }
]

console.log(bubbleSort(students.map(x => x.age)))

You might be able to use one of these two bubble sort algorithms

Using a foreach and a for loop

let bubbleSort = (inputArr) => {
    let len = inputArr.length;
    inputArr.forEach(function(i){
        for (let j = 0; j < len; j++) {
            if (inputArr[j] > inputArr[j + 1]) {
                let tmp = inputArr[j];
                inputArr[j] = inputArr[j + 1];
                inputArr[j + 1] = tmp;
            }
        }
    })

    return inputArr;
};

Using a foreach and a do/while loop

let bubbleSort = (inputArr) => {
    let len = inputArr.length;
    let swapped;
    do {
        swapped = false;
          inputArr.forEach(function(number, i){
            if (inputArr[i] > inputArr[i + 1]) {
                let tmp = inputArr[i];
                inputArr[i] = inputArr[i + 1];
                inputArr[i + 1] = tmp;
                swapped = true;
            }
          })
    } while (swapped);
    return inputArr;
};

https://www.studytonight.com/data-structures/bubble-sort

Terrence
  • 131
  • 5
-1

To sort any data it's requred minimum O(nlogn) operations, but forEach func will called only n times. You hence unable to do it using forEach only.

But if you really want to use forEach only, let's use some magic with async js:

var students = [{
    name: 'Mark',
    age: 28
  },
  {
    name: 'Johnson',
    age: 30
  },
  {
    name: 'Williams',
    age: 12
  },
  {
    name: 'Henry',
    age: 27
  }
]

students.forEach(fun)

function fun(currentValue) {
  setTimeout(() => console.log(currentValue), currentValue.age);
}

Please, don't use this solution in production

Mikhail Leliakin
  • 176
  • 1
  • 11