---
id: 694e1931f6509ae16aa1c36f
title: Step 18
challengeType: 1
dashedName: step-18
---

# --description--

Mission control has alerted you that the list of EVA-eligible astronauts should also be sorted by priority descending. There are a few ways to sort an array - perhaps the most basic is `bubble sort`.

`Bubble sort` works by repeatedly stepping through a list, comparing neighboring items, and swapping them if they’re in the wrong order. After each pass, the item that should come first based on your sort criteria moves closer to (or “bubbles” toward) its correct position in the array. Here is how you can sort `crew` by priority descending using `bubble sort`:

```js
// Outer loop: controls how many passes we make
for (let i = 0; i < crew.length - 1; i++) {
  // Inner loop: compares neighboring items
  for (let j = 0; j < crew.length - 1 - i; j++) {
    // If current member has lower priority than next, swap
    if (crew[j].priority < crew[j + 1].priority) {
      // Using a temp variable for the swap
        const temp = crew[j];
        crew[j] = crew[j + 1];
        crew[j + 1] = temp;
    }
  }
}
```

Create a new helper function named `sortByPriorityDescending` that accepts a `crew` parameter. This function should directly sort the input `crew` array by priority descending, use two nested `for` loops, and should not return anything. 

# --hints--

You should have a function named `sortByPriorityDescending` with a `crew` parameter.

```js
assert.isFunction(sortByPriorityDescending);
const regex = __helpers.functionRegex('sortByPriorityDescending', ['crew']);
const cleaned = __helpers.removeJSComments(sortByPriorityDescending.toString());
assert.match(cleaned, regex);
```

Your `sortByPriorityDescending` function should use two nested `for` loops to perform the sort.

```js
const loopMatches = sortByPriorityDescending.toString().match(/for\s*\(/g) || [];
assert.isAtLeast(loopMatches.length, 2);
```

Your `sortByPriorityDescending` function should sort the input `crew` by `priority` in descending order.

```js
const crewSample = [
  { name: "A", priority: 2 },
  { name: "B", priority: 5 },
  { name: "C", priority: 3 }
];
sortByPriorityDescending(crewSample);
assert.deepEqual(crewSample.map(a => a.name), ["B", "C", "A"]);
```

Your `sortByPriorityDescending` function should directly sort the input `crew` array in-place and not return a new array.

```js
const crewSample2 = [
  { name: "X", priority: 1 },
  { name: "Y", priority: 2 }
];
const result = sortByPriorityDescending(crewSample2);
assert.strictEqual(result, undefined);
assert.deepEqual(crewSample2.map(a => a.priority), [2, 1]);
```

# --seed--

## --seed-contents--

```js
const squad = [];

const firstAstronaut = {
  id: 1,
  name: "Andy",
  role: "Commander",
  isEVAEligible: true,
  priority: 3
};

function addCrewMember(crew, astronaut) {
  for (let i = 0; i < crew.length; i++) {
    if (crew[i].id === astronaut.id) {
      console.log("Duplicate ID: " + astronaut.id);
      return;
    }
  }
  crew.push(astronaut);
}

addCrewMember(squad, firstAstronaut);

const remainingCrew = [
  { id: 2, name: "Bart", role: "Pilot", isEVAEligible: false, priority: 8 },
  { id: 3, name: "Caroline", role: "Engineer", isEVAEligible: true, priority: 4 },
  { id: 4, name: "Diego", role: "Scientist", isEVAEligible: false, priority: 1 },
  { id: 5, name: "Elise", role: "Medic", isEVAEligible: true, priority: 7 },
  { id: 6, name: "Felix", role: "Navigator", isEVAEligible: true, priority: 6 },
  { id: 7, name: "Gertrude", role: "Communications", isEVAEligible: false, priority: 4 },
  { id: 8, name: "Hank", role: "Mechanic", isEVAEligible: true, priority: 2 },
  { id: 9, name: "Irene", role: "Specialist", isEVAEligible: true, priority: 5 },
  { id: 10, name: "Joan", role: "Technician", isEVAEligible: false, priority: 1 },
];

for (let i = 0; i < remainingCrew.length; i++) {
  addCrewMember(squad, remainingCrew[i]);
}

function swapCrewMembers(crew, fromIndex, toIndex) {
  if (
    fromIndex < 0 || 
    toIndex < 0 ||
    fromIndex >= crew.length ||
    toIndex >= crew.length
  ) {
    console.log("Invalid crew indices");
    return;
  }

  const updatedCrew = crew.slice();
  updatedCrew[fromIndex] = updatedCrew.splice(toIndex, 1, updatedCrew[fromIndex])[0];

  return updatedCrew; 
}

const updatedSquad = swapCrewMembers(squad, 2, 5);

--fcc-editable-region--

--fcc-editable-region--

function getEVAReadyCrew(crew) {
  const eligible = [];
  for (const astronaut of crew) {
    if (astronaut.isEVAEligible) eligible.push(astronaut);
  }
  
  return eligible;
}
```
