Ask coding questions

← Back to all posts
[JS,PY] How do I turn this into Javascript?
MisledWater79 (0)

I'm trying to make this code in javascript. What it does is take two lists and sort them. The index of users is the same to the index of scores and I want it to stay like that. This code sorts Users based on how Scores would sort from. It then sorts the scores.

users = ['bob','bill','joe']
scores = [325,15,6784]
sorted(users, reverse=True, key=lambda x: scores[users.index(x)])
sorted(scores, reverse=True)
Comments
hotnewtop
Coder100 (18925)

use the sort method

let users = ["bob", "bill", "joe"];
let scores = [325, 15, 6784];

users = users.sort((a, b) => scores[a] - scores[b]);
scores = scores.sort();
MisledWater79 (0)

@Coder100 I tried it and it didn't sort the string properly.

MisledWater79 (0)

@Coder100 It still seems the javascript strings isn't right

Coder100 (18925)

oops i made a mistake there you go @MisledWater79

MisledWater79 (0)

@Coder100 It works now thanks! I do have 1 more question how do I turn a list of strings into ints? I didn't realize my list was strings not ints.

Coder100 (18925)

@MisledWater79 well you can't convert bob to a number silly

MisledWater79 (0)

@Coder100 No I have strings like "515632" in a list and I want to convert those to ints