javascript - Sorting an Array according to the order of another Array -
I have an array with such shapes:
var arr = [ 'Small', 'small', 'small', 'small', ... 'medium', 'medium', 'medium', 'medium', ... 'big', 'big', ...] ; I need to reconstruct this array according to this command:
var command = ['short', 'small', ' Medium ',' medium ',' big ']; Then the result ends:
var arr = ['short', 'small', 'medium', 'medium', ' Big ',' small ',' small ',' medium ',' medium ',' big '...]; I know about other such questions, but I could not find anything yet. I am uncertain about how to contact this. I was thinking sorting but for whom should I test? It sounds simple but I'm stuck, do not know where to start. Any signals?
Just define scorer for your sort method. This is your code. Try it
var init_arr = ['small', 'big', 'big', 'medium']; Var scorer = {small: 0, medium: 1, big: 2} // You can also use the same array I am creating a new var final_arb = init_arr.sort (function (a, b) {return scorer [ A] -Skor [B];}); Warning (final_arr); // small, medium, large, large
Comments
Post a Comment