0

I have the following TypeScript code:

export const cgGroups = [
  {
    id: 2,
    name: 'North America & Caribbean'
  },
  {
    id: 3,
    name: 'Latin America'
  },
  {
    id: 6,
    name: 'Europe'
  },
  {
    id: 4,
    name: 'Asia Pacific'
  },
  {
    id: 1,
    name: 'Middle East & Africa'
  },
  {
    id: 7,
    name: 'International'
  }
];

I want to sort the above alphabetical except one object

{
   id: 7,
   name: 'International'
}

which I want to move it to the last of the sorted array.

I tried the below code to sort:

cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
        if (a.name.toLowerCase() > b.name.toLowerCase()) {
          return 1;
        }
        if (a.name.toLowerCase() < b.name.toLowerCase()) {
          return -1;
        }
        return 0;
      });

Here is the expected output:

Asia Pacific, Europe, Latin America, Middle East & Africa, North America & Caribbean, and International

Can anyone guide me here to fix this issue?

5
  • 2
    If a.name === "International" return -1 and other if statements can follow this one. But your expected output doesn't make sense, Asia should come first (sorted alphabeticaly by name) Commented May 23, 2018 at 14:36
  • You said you want to sort alphabetically, shouldn't Asia Pacific be the first entry? Commented May 23, 2018 at 14:38
  • Sorry I have corrected it now. Thanks a lot for pointing it out :) Commented May 23, 2018 at 14:57
  • It doesn't work because you didn't encode the condition about the item with id: 7 into the comparison function. Commented May 23, 2018 at 15:44
  • There is no TypeScript involved in this question. It is pure JavaScript, the code you posted works in the browser. Commented May 23, 2018 at 16:02

2 Answers 2

2

It doesn't work because you didn't encode the condition about the item with name: 'International' into the comparison function.

It could be like this:

cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
    if (a.name.toLowerCase() == 'international') {
        return +1;      // "a" is the greatest element of the array
    } else if (b.name.toLowerCase() == 'international') {
        return -1;      // "a" stays before "b" because "b" is the last item
    } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
        return 1;       // regular items, compare their names
    } else if (a.name.toLowerCase() < b.name.toLowerCase()) {
        return -1;
    }

    return 0;
});
Sign up to request clarification or add additional context in comments.

Comments

1

If you don't mind adding a method to the Array prototype, these are two potential solutions (the first one modifies the original array, while the second one returns a new array).

let cgGroups = [
    {
        id: 2,
        name: 'North America & Caribbean'
    },
    {
        id: 3,
        name: 'Latin America'
    },
    {
        id: 6,
        name: 'Europe'
    },
    {
        id: 4,
        name: 'Asia Pacific'
    },
    {
        id: 1,
        name: 'Middle East & Africa'
    },
    {
        id: 7,
        name: 'International'
    }
];

const sortAlph = (a, b) => {
    if (a.name.toLowerCase() > b.name.toLowerCase()) {
        return 1;
    }
    if (a.name.toLowerCase() < b.name.toLowerCase()) {
        return -1;
    }
    return 0;
}

Array.prototype.move = function (fromIndex, toIndex) {
  let element = this[fromIndex];
  this.splice(fromIndex, 1);
  this.splice(toIndex, 0, element);
}

Array.prototype.moveToTheEnd = function(index) {
  let element = this[index];
  return this.filter(x => x !== element).concat(element);
}

cgGroups
    .sort(sortAlph)
    .move(cgGroups.findIndex(x => x.name === 'International'), cgGroups.length)

newArr = cgGroups
  .sort(sortAlph)
  .moveToTheEnd(cgGroups.findIndex(x => x.name === 'International'))

console.log(cgGroups);
console.log(newArr);

1 Comment

Thanks a lot for the solution. This helped me to fix the issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.