0

So, I've got a nice array like so:

["Mexico", "Belize", "Canada", "US"]

I want to sort it north-to-south.

I want:

["Canada", "US", "Mexico", "Belize"]

How to do this in javascript?

I have a preset of countries. I can tell Javascript "Hey, that array of countries I just gave you? Sort it Canada, US, Mexico, Belize."

This is for a static graphic, won't have to be dynamic, hard-coding OK.

I've got: my_array.sort() But I have no idea what to do next. Everything online seems to sort by number or alphabet.

And FWIW, list of countries is just an example. In real life, I'm trying to sort sources of government funding: state, federal, trust fund, tobacco settlement. But that's a pretty abstract question. Better to use something concrete like countries, no?

Maggie
  • 1,975
  • 3
  • 15
  • 17
  • 1
    You will first need to find out the countries' latitudes, either through a list you created, or using a service like Google Maps. Which way would you rather go? Do you have a preset of countries, or can new ones be added? – blex Aug 28 '14 at 13:32
  • You need to first give JavaScript some idea of precisely how far South something is; that or create some form of artificial intelligence capable of looking up the information for itself. – David Thomas Aug 28 '14 at 13:32
  • 2
    How in the world would JavaScript know that? Do you have a look up table of locations? – epascarello Aug 28 '14 at 13:32
  • OP, try something first and we'll help you then, that's how it works here. By the way: so many down votes, come on SO you're better than that, the question is interesting... – axelduch Aug 28 '14 at 13:34
  • Find out the countries latitude, add the whole lot into an array of object literals containing the country names and their latitude, then you will be able to calculate which one belongs where and produce an output array of just the country names. – Mark Walters Aug 28 '14 at 13:34
  • Maggie, In order to sort it needs context that you supply to it. JavaScript does not read minds or it would make my life as a developer so much easier and make me richer. Show a real example with some filler data. This is what you want: http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript – epascarello Aug 28 '14 at 13:41
  • possible duplicate of [How to sort an array in JavaScript in a customized order?](http://stackoverflow.com/questions/10906337/how-to-sort-an-array-in-javascript-in-a-customized-order) – Raymond Chen Aug 28 '14 at 13:43
  • possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Peter O. Aug 28 '14 at 17:25

1 Answers1

2

You might create a function that returns the latitude of a country like:

function getLatitude(country) {
  var countries = {
   Belize : 17.0667,
   Canada : 45.4,
   Mexico : 19,
   US : 38.8833 // Should this be America?
  }
  return countries[country];
}

however you need to work out what the latitude is—perhaps it's a centroid? Maybe it's from a service that provides the latitude of locations?

Then to sort them:

["Mexico", "Belize", "Canada", "US"].sort(function(a, b) {
  return getLatitude(a) - getLatitude(b);
});
// ["Belize", "Mexico", "US", "Canada"]

You need to work out how to handle locations that you can't get a latitude for, should they be removed or sorted to one end or the other?

RobG
  • 142,382
  • 31
  • 172
  • 209
  • OK, so it seems like I can't supply it a custom list of strings. I will need to add some marker, some numerical marker, to sort by. That is easy enough. Thanks! – Maggie Aug 28 '14 at 13:45
  • You may be able to find a service, like [*Maps of the World*](http://www.mapsofworld.com/lat_long/belize-lat-long.html). – RobG Aug 28 '14 at 13:47