-3

I have a list of coordinates of (x, y) pair, and I want to create a new list of coordinates that's sorted by the x-value in ascending order. How can I achieve this using d3/javascript (I just started learning a few days ago)? I've found that stack.order() could be useful, but I'm not sure. Thanks so much!

LinAlg
  • 1
  • 1

1 Answers1

-1

assuming your coordinates array looks like this:

var coordinates = [
    { x: 3, y: 2},
    { x: 1, y: 1}
}

you can use:

coordinates.sort(function(a, b){
    return a.x - b.x;
})

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Matthijs Brouns
  • 2,299
  • 1
  • 27
  • 37