3

Sorry if this is a stupid question, but I'm learning JavaScript and was wondering if there was an easy way to sort 2 lists like the ones here:

var names=["item", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"];
var points=[12, 12345, 5765, 123, 3, 567765, 99, 87654, 881, 101];

How would I display the items from 'names' according to the corresponding value in 'points'? For example, for the above item6 would displayed first and item5 would be displayed last.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • 2
    Doesn't appear to be any obvious or intuitive relationship between the two. Please explain in more detail – charlietfl Jun 27 '17 at 23:29
  • Do any of these answer your question? https://stackoverflow.com/q/37063224/1048572 https://stackoverflow.com/q/14004188/1048572 https://stackoverflow.com/q/16759404/1048572 – Bergi Jun 27 '17 at 23:33

3 Answers3

2

I don't know if it's easy enough, but you could make an array of objects, sort it by the value prop and just map to get only the name props.

let names = ["item", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"],
    points = [12, 12345, 5765, 123, 3, 567765, 99, 87654, 881, 101],
    
    res = names.map((v, i) => ({ name: v, val: points[i] }))
               .sort((a, b) => b.val - a.val)
               .map(v => v.name);
    
    console.log(res);
kind user
  • 40,029
  • 7
  • 67
  • 77
0

Here's a somewhat lengthy solution (with a much more concise version below). The basic idea is to:

  1. Sort the points array in descending order
  2. Loop through the sorted array, and find each value's position in the original points array
  3. Grab the corresponding item from the names array
  4. Push the value into a new array

var names=["item", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"];
var points=[12, 12345, 5765, 123, 3, 567765, 99, 87654, 881, 101];

const sortedPoints = points.slice().sort(function(a, b) {
  return b - a;
});

const sortedNames = [];
sortedPoints.forEach(function(val) {
  const position = points.indexOf(val);
  sortedNames.push(names[position]);
})

console.log(sortedNames)

For a more concise solution, following the same process above but taking advantage of some shortcuts:

const names = ["item", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"];
const points = [12, 12345, 5765, 123, 3, 567765, 99, 87654, 881, 101];

const sortedNames = points.slice().sort((a, b) => b - a).map(val => names[points.indexOf(val)]);

console.log(sortedNames)
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
0

Javascript doesn't have a zip function natively. But that is most of what you want to do here. A little utility library like underscore is pretty handy. You can view the annotated source if you just want to replicate a zip function yourself.

var zippedAndSorted = _.zip(names, points)
.sort(function(a, b) {
    return b - a;
});

Then you can iterate over each pair:

zippedAndSorted.forEach(function(namePoint) {
    console.log('Name: ' + namePoint[0] + ' Points: ' + namePoint[1]);
});
gvfordo
  • 189
  • 1
  • 10