Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic Sorting Loop

I am trying to write a function which would loop through a list of positions in an array(Position Array) and for each position create an array in the Position Array. And then loop through another array with candidates with different positions and sort them into the arrays with the same positions within the Position Array.

This is what I have been able to do, I want to be able to make it dynamic, but do not know how to go about it;

Here’s my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let positionArr = ["Chairman", "Secretary", "Organiser", 
"Finacial Secretary" ];
let varPositions = [];

let candidates = [
{
  name: "Kwesi",
  position: "Finacial Secretary",
},
{
  name: "Kofi",
  position: "Chairman",
},
{
  name: "Ama",
  position: "Secretary",
},
{
  name: "Kwame",
  position: "Finacial Secretary",
},
{
  name: "Juliet",
  position: "Organiser",
},
{
  name: "Jese",
  position: "Chairman",
},
];

const sort = () => {
  let position;
  positionArr.forEach((element) => {
  element = new Array();
  varPositions.push(element);
});

candidates.forEach((elementPos) => {
position = elementPos.position;

positionArr.forEach((element) => {
  if (element === position) {
    if (position === positionArr[0]) {
      varPositions[0].push(elementPos);
    } else if (position === positionArr[1]) {
      varPositions[1].push(elementPos);
    } else if (position === positionArr[2]) {
      varPositions[2].push(elementPos);
    } else if (position === positionArr[3]) {
      varPositions[3].push(elementPos);
    }
  }
 });
});
  console.log(varPositions);
};

sort();

Thanks in Advance 🙂

>Solution :

If all you’re trying to do is sort your candidates based on the positions array, it is much much simpler than you have there. You simply sort by the index in the array

const result = candidates.sort((a,b)  => 
    positionArr.indexOf(a.position) 
           - positionArr.indexOf(b.position)
)

Live example:

let positionArr = ["Chairman", "Secretary", "Organiser", 
"Finacial Secretary" ];

let candidates = [
{
  name: "Kwesi",
  position: "Finacial Secretary",
},
{
  name: "Kofi",
  position: "Chairman",
},
{
  name: "Ama",
  position: "Secretary",
},
{
  name: "Kwame",
  position: "Finacial Secretary",
},
{
  name: "Juliet",
  position: "Organiser",
},
{
  name: "Jese",
  position: "Chairman",
},
];

const result = candidates.sort((a,b)  => 
    positionArr.indexOf(a.position) 
           - positionArr.indexOf(b.position)
)

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading