3

I have two arrays

  1. MRP [60 80 82 50 80 80 ]
  2. Brand [ A B C D E F ]

I need to get top two brands based on MRP. highest two in MRP is 82 ,80 but 80 is 3 times repeated so i need all repeated values as well to get top two brands. i.e i need to display [C B E F]

For that i sorted MRP in descending order.Now MRP after sorting become [ 82 80 80 60 50 ]

Now i need to sort Brand Array based on Sorted MRP.Can some one help in this sorting using javascript .

2
  • 1
    Any attempts on your own so far? Is this Java or JavaScript? Commented Jun 13, 2014 at 8:54
  • @ZiNNED I only sorted the first array ie MRP bt not the brands.but now i got the solution shared by Nishanthi Grashia in the subsequent post. Commented Jun 13, 2014 at 9:45

2 Answers 2

9

Try below code

var A = [60, 80, 82, 50, 80, 80];
var B = ['a', 'b', 'c', 'd', 'e', 'f'];

var all = [];

for (var i = 0; i < B.length; i++) {
    all.push({ 'A': A[i], 'B': B[i] });
}

all.sort(function(a, b) {
  return b.A - a.A;
});

A = [];
B = [];

for (var i = 0; i < all.length; i++) {
   A.push(all[i].A);
   B.push(all[i].B);
}    

alert(A);
alert( B);
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help! Please also upvote and accept as answer if it helped.
0

Using lodash/underscore:

var A = [60, 80, 82, 50, 80, 80];
var B = ['a', 'b', 'c', 'd', 'e', 'f'];

var result = _.unzip(_.sortBy(_.zip(A, B), '-0'));

var A_sorted = result[0]; // [82, 80, 80, 80, 60, 50]
var B_sorted = result[1]; // ["c", "b", "e", "f", "a", "d"]]

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.