5

I have an array of strings:

var players = [{Name: player1name, Surname: player1surname, Position: "Centre back"}, {Name: player2name, Surname: player2surname, Position: "Striker"}, {Name: player3name, Surname: player3surname, Position: "Full back"}, {Name: player4name, Surname: player4surname, Position: "Goalkeeper"}, {Name: player5name, Surname: player5surname, Position: "Midfielder"}, {Name: player6name, Surname: player6surname, Position: "Winger"}];

Order of an array items is not always the same. I want to sort that array so it goes like this: Goalkeeper, Full back, Centre back, Midfielder, Winger, Striker. I'm thinking about enums but I don't know how to use them in this situation.

5
  • 2
    If you know the desired order, then why don't you define the array in that order? Commented Aug 24, 2017 at 17:33
  • Which logic you want to order ? Commented Aug 24, 2017 at 17:34
  • @trincot I can't because I get players from database for specific squad and players aren't sorted when I get them. Every player has property "Position". Commented Aug 24, 2017 at 17:36
  • It would make the question more interesting if you would include the player values, because as it is now (with only positions), it looks like a problem that is not there. Commented Aug 24, 2017 at 17:38
  • @trincot I have edited a question. I will try an answer below. If you have more simple solution feel free to suggest it. Commented Aug 24, 2017 at 17:48

2 Answers 2

5

You could take an object for the sort order and then sort by the difference.

var players = ["Centre back", "Striker", "Full back", "Goalkeeper", "Midfielder", "Winger"],
    order = { Goalkeeper: 1, 'Full back': 2, 'Centre back': 3, Midfielder: 4, Winger: 5, Striker: 6 };
    
players.sort(function (a, b) {
    return order[a] - order[b];
});

console.log(players);

Sign up to request clarification or add additional context in comments.

6 Comments

If he can manually define the order with an object, he could just change the array itself :D anyway good one. Have an upvote.
i think it's just a simplified array, for later use with more data in an object.
Yeah. Can think of that case.
Is it possible to use that logic on array of objects that have property 'position'?
@user7479651, it is possible with return order[a.position] - order[b.position];
|
0

There you go. You should use this approach. You need to create a ranking map and then sort by that map.

let players = [
    {
    "name": "Molla Wague",
    "position": "Centre-Back",
    "jerseyNumber": 13,
    "dateOfBirth": "1991-02-21",
    "nationality": "Mali",
    "contractUntil": "2018-06-30",
    "marketValue": null
    },
    {
    "name": "Heurelho Gomes",
    "position": "Keeper",
    "jerseyNumber": 1,
    "dateOfBirth": "1981-02-15",
    "nationality": "Brazil",
    "contractUntil": "2019-06-30",
    "marketValue": null
    },
    {
    "name": "Christian Kabasele",
    "position": "Centre-Back",
    "jerseyNumber": 27,
    "dateOfBirth": "1991-02-24",
    "nationality": "Belgium",
    "contractUntil": "2021-06-30",
    "marketValue": null
    },
    {
    "name": "José Holebas",
    "position": "Left-Back",
    "jerseyNumber": 25,
    "dateOfBirth": "1984-06-27",
    "nationality": "Greece",
    "contractUntil": "2020-06-30",
    "marketValue": null
    },
    {
    "name": "Daryl Janmaat",
    "position": "Right-Back",
    "jerseyNumber": 2,
    "dateOfBirth": "1989-07-22",
    "nationality": "Netherlands",
    "contractUntil": "2020-06-30",
    "marketValue": null
    },
    {
    "name": "Étienne Capoue",
    "position": "Defensive Midfield",
    "jerseyNumber": 29,
    "dateOfBirth": "1988-07-11",
    "nationality": "France",
    "contractUntil": "2019-06-30",
    "marketValue": null
    },
    {
    "name": "Tom Cleverley",
    "position": "Central Midfield",
    "jerseyNumber": 8,
    "dateOfBirth": "1989-08-12",
    "nationality": "England",
    "contractUntil": "2022-06-30",
    "marketValue": null
    },
    {
    "name": "Roberto Pereyra",
    "position": "Attacking Midfield",
    "jerseyNumber": 37,
    "dateOfBirth": "1991-01-07",
    "nationality": "Argentina",
    "contractUntil": "2021-06-30",
    "marketValue": null
    },
    {
    "name": "Troy Deeney",
    "position": "Centre-Forward",
    "jerseyNumber": 9,
    "dateOfBirth": "1988-06-29",
    "nationality": "England",
    "contractUntil": "2021-06-30",
    "marketValue": null
    }
];
const rankMap = {
	'Keeper': 1,
    'Centre-Back': 2,
    'Right-Back': 3,
    'Left-Back': 4,
    'Defensive Midfield': 5,
    'Central Midfield': 6,
    'Attacking Midfield': 7,
    'Right Wing': 8,
    'Left Wing': 9,
    'Centre-Forward': 10
};

players = players.sort((a, b) => rankMap[a.position] - rankMap[b.position]);

console.log(players);

Comments

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.