0

I am trying to sort a dataset by the string rank based on their location in an array. I tried using indexOf with sort to limited success. I did find another post similar, although it is not relating to my question for any quick duplicate reporters.

Dataset:

[ { "_id": { "$oid": "5b535f6eddfad00564b103db" }, "notes": [ "Very attractive" ], "warns": [], "username": "Saddy", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg", "id": "76561198151478478", "dateHired": { "$date": "2018-07-21T16:29:34.563Z" }, "profile": "https://steamcommunity.com/id/dpitt/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Administrator", "__v": 0 }, { "_id": { "$oid": "5b54d085ed4855275f4cea63" }, "notes": [], "warns": [], "username": "meme master nick", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg", "id": "76561198353773365", "dateHired": { "$date": "2018-07-22T18:44:21.814Z" }, "profile": "https://steamcommunity.com/profiles/76561198353773365/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Owner", "__v": 0 }, { "_id": { "$oid": "5b567169e7179a32d9cc2abe" }, "notes": [], "warns": [], "username": "DarkGamer2k16", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg", "id": "76561194018842253", "dateHired": { "$date": "2018-07-21T16:29:34.563Z" }, "profile": "https://steamcommunity.com/id/DarkGamer2k16/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Moderator", "__v": 0 } ] 

Failed Attempts:

console.log(arr.sort(function(a){ return ["Owner", "Administrator", "Moderator"].indexOf(a.rank) })) arr.sort(function(a,b){ let sort = Object.assign({}, a, b); return["Owner", "Administrator", "Moderator"].indexOf(sort.rank) }) 

both of the above are just replacing all data with mine. Thank you for your assistance

Expected Output:

[ { "_id": { "$oid": "5b54d085ed4855275f4cea63" }, "notes": [], "warns": [], "username": "meme master nick", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg", "id": "76561198353773365", "dateHired": { "$date": "2018-07-22T18:44:21.814Z" }, "profile": "https://steamcommunity.com/profiles/76561198353773365/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Owner", "__v": 0 }, { "_id": { "$oid": "5b535f6eddfad00564b103db" }, "notes": [ "Very attractive" ], "warns": [], "username": "Saddy", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg", "id": "76561198151478478", "dateHired": { "$date": "2018-07-21T16:29:34.563Z" }, "profile": "https://steamcommunity.com/id/dpitt/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Administrator", "__v": 0 }, { "_id": { "$oid": "5b567169e7179a32d9cc2abe" }, "notes": [], "warns": [], "username": "DarkGamer2k16", "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg", "id": "76561194018842253", "dateHired": { "$date": "2018-07-21T16:29:34.563Z" }, "profile": "https://steamcommunity.com/id/DarkGamer2k16/", "enactedBans": 0, "currentTickets": 0, "totalTickets": 0, "rank": "Moderator", "__v": 0 } ] 
2
  • What's the expected output look like?CommentedJul 24, 2018 at 1:00
  • I'll update it in the post, one second.
    – Saddy
    CommentedJul 24, 2018 at 1:01

1 Answer 1

3

You need to use both a and b in the arguments provided to sort - compare by the difference of their indicies in the ["Owner", "Administrator", "Moderator"] array.

const input=[{"_id":{"$oid":"5b535f6eddfad00564b103db"},"notes":["Very attractive"],"warns":[],"username":"Saddy","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg","id":"76561198151478478","dateHired":{"$date":"2018-07-21T16:29:34.563Z"},"profile":"https://steamcommunity.com/id/dpitt/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Administrator","__v":0},{"_id":{"$oid":"5b54d085ed4855275f4cea63"},"notes":[],"warns":[],"username":"meme master nick","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg","id":"76561198353773365","dateHired":{"$date":"2018-07-22T18:44:21.814Z"},"profile":"https://steamcommunity.com/profiles/76561198353773365/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Owner","__v":0},{"_id":{"$oid":"5b567169e7179a32d9cc2abe"},"notes":[],"warns":[],"username":"DarkGamer2k16","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg","id":"76561194018842253","dateHired":{"$date":"2018-07-21T16:29:34.563Z"},"profile":"https://steamcommunity.com/id/DarkGamer2k16/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Moderator","__v":0}] const ranks = ["Owner", "Administrator", "Moderator"]; input.sort((a, b) => ranks.indexOf(a.rank) - ranks.indexOf(b.rank)); console.log(input);

1
  • Thank you! I have been stuck on it for a while now. Once I can I will mark your as correct.
    – Saddy
    CommentedJul 24, 2018 at 1:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.