0

I am facing this problem in javascript : I have lot of random map coordinates(latitude,longitude) stored in an array like

var coordinates = [
[64,22],[55,33],[28,35],[...,...]
]

I also have a method that calculates distance between two of those points

like

var getDistance =function(point1,point2)
    {
    return L.dist(point1,point2);//leaflet method
    }

then here is my problem :

how to sort my array to have the distance sorted by closest from first coordinate then closest to second, then closest to third, then closest to fourth... ? anyone have a solution for this... im lost :(

edit 1 :

I tried to resolve the problem with a nested for loop ... but the results seems to be wrong.

var cloestIndex = 1;
var closestDistance = 99999999;

     for (var i = 0; i < coords.length; i++) {
         for (var j = i + 1; j < coords.length; j++) {
             if ((Map().distance(coords[i], coords[j]) < closestDistance) &&
                                (Map().distance(coords[i], coords[j]) != 0){

                  closestDistance = (Map().distance(coords[i], coords[j]));

                            closestIndex = j;
                  }
               }
           console.log("CD", closestDistance + "(" + closestIndex + ")");
           finalArray.push(coords[closestIndex]);
           coords.splice(closestIndex, 0);
           cloestDistance = 9999999;
      }
Noj
  • 164
  • 1
  • 15
  • Possible duplicate of [How to sort array items by longitude latitude distance in javascripts?](http://stackoverflow.com/questions/26836146/how-to-sort-array-items-by-longitude-latitude-distance-in-javascripts) – James111 Aug 30 '16 at 23:57
  • Are you saying that the element that is first before sorting should still be first after sorting but just the other elements should be rearranged to form the shortest total path between all points? Or do you want to find the two elements that are closest to each other and make those the first two in the result? If the latter, what if the shortest distance is between points A and B but the second shortest distance is between points D and E? – nnnnnn Aug 31 '16 at 00:00
  • What does "closest to the n-th" mean if you are sorting? – Oriol Aug 31 '16 at 00:00
  • 1
    Please follow basic international English standards. The first letter of a sentence is capitalized. The word "I" is capitalized. We write contractions such as "I'm" with an apostrophe. Also, please indent your code properly. Spellcheck your post; for example, correct spelling is "closest", not "cloest". Paste the **actual** code you are using; code with the invalid keyword `retrun` would not even run, and the function (not method) definition must begin with a `function` keyword. This is not a chat channel or an SMS conversation. –  Aug 31 '16 at 03:46
  • @torazaburo I edited it, my bad. – Noj Aug 31 '16 at 13:39

1 Answers1

1

how to sort my array to have the distance sorted by cloest from first coordinate then cloest to second, then cloest to third, then cloest to fourth... ?

That's not called a "sort" because "closest distance to previous" is not an ordering. Sorting them by distance to a single point would be.

So to do what you want, you really should literally follow your description and first put the first coordinate in the result array, then find the closest-to-that in the rest and add it, then find the closest-to-that and so on until no more coordinates are left.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • But It would be possible that the nearest-to-that coordinate is some coordinate previously added, injecting duplicates, some kind of control in that would be needed, am i wrong? – Jose Hermosilla Rodrigo Aug 31 '16 at 01:02
  • @JoseHermosillaRodrigo: Yes, of course. That's why I wrote "*find … in the rest, …until no more are left*". – Bergi Aug 31 '16 at 01:08
  • Yeep. I didn't think about this enought, sorry!! :) – Jose Hermosilla Rodrigo Aug 31 '16 at 01:14
  • 1
    What happened to your New Year's resolution not to answer poorly-written questions that the OP will never come back to clarify or respond to comments/answers on? –  Aug 31 '16 at 04:38
  • 1
    @torazaburo It might look a bit dislexic, but I found it well-written enough to be answerable. I can't know whether the OP will come back, and the comments are basically only pointing out the same problem as my answer (actually my answer started as a comment). But sure, the question would need to have much higher quality to get an answer with a full-fledged implementation… – Bergi Aug 31 '16 at 04:48
  • thanks man, stackoverflow got too many people that think we are all native english speakers, over fluents, they also think that we make no efforts to formulate our questions ... – Noj Aug 31 '16 at 13:26
  • @Noj You should however [fix the question](http://stackoverflow.com/posts/39238727/edit) after the mistakes were pointed out :-) – Bergi Aug 31 '16 at 13:27
  • i tried to understand but i dont know what to put :( – Noj Aug 31 '16 at 13:29
  • @Noj: Start with the typos from torazaburos comment – Bergi Aug 31 '16 at 13:30