Skip to content
← Back to Community
[JS,PY] How do I turn this into Javascript?
Profile icon
MisledWater79

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)
Voters
Profile icon
MisledWater79
Comments
hotnewtop
Profile icon
Coder100

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();
Profile icon
MisledWater79

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

Profile icon
Coder100

what went wrong @MisledWater79

Profile icon
Coder100
Profile icon
MisledWater79

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

Profile icon
Coder100

oops i made a mistake there you go @MisledWater79

Profile icon
MisledWater79

@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.

Profile icon
Coder100

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

Profile icon
MisledWater79

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