Belka
Belka

Reputation: 11

Javascript array sort issue

Having a problem with array of objects sort in Javascript. Sorting array of objects that have lat, lng, and trying to sort by distance from certain point. Object looks like this(not a full object but you get the idea)

0: Object
 lat: 27.267653
 lng: -81.351229
 price: 214.29
 title: "Lake Front Home"

Sorting using :

set.sort(sortDistanceLow);

var sortDistanceLow = function(a,b)
{
    var aDist = geoLib.distance(a.lat,a.lng, sortOrigin.lat,sortOrigin.lng);
    var bDist = geoLib.distance(b.lat,b.lng, sortOrigin.lat,sortOrigin.lng);
    return (aDist-bDist);
}

The result of the sort has 2 items out of order and for the life of me I can't figure out why. Look at index 0 and 21:

Dist for - 0 Lake Front Home is - 1.8974297528647823
Dist for - 1 Waterfront Property for Rent is - 0
Dist for - 2 The Terrace On Lake McCoy 310 is - 0.6564731929026136
Dist for - 3 The Terraces on Lake McCoy 309 is - 0.6606015018504228
Dist for - 4 A+ Reviews, Lakefront Townhome w/pool, n is - 0.9397875614447686
Dist for - 5 A Brand New Luxurious Lakefront Townhous is - 1.004205821447096
Dist for - 6 Simplicity Cottage Lakeside Bungalow is - 1.0611283822559001
Dist for - 7 Spend Some Time In A Quiet Secluded Spot is - 1.073466614941598
Dist for - 8 Dockside Apartment is - 1.073466614941598
Dist for - 9 Field of Dreams Lakefront Paradise    is - 1.1013500555209488
Dist for - 10 Beautiful Lake June Home is - 1.1499388283994212
Dist for - 11 Beautiful Lake June Home is - 1.2739374435782866
Dist for - 12 Lakefront Condo on Lake Clay is - 1.3737081520111232
Dist for - 13 Spacious Lake Front Home on Lake June ~  is - 1.378625813859494
Dist for - 14 Lake Front Retreat, Lake Placid FL-Water is - 1.3858006411581065
Dist for - 15 Tranquil Retreat, Boating, Fishing, Away is - 1.4176726711713614
Dist for - 16 Sunsets on Lake June - Immaculate and Up is - 1.47195380295702
Dist for - 17 5 bedroom Peaceful Lakeside Retreat is - 1.4900487697270766
Dist for - 18 Dales lakeside Resort  Waterfront Renta is - 1.743581215913245
Dist for - 19 Lakefront Home on Beautiful Lake Placid is - 1.853756967222272
Dist for - 20 2/1 Lake Cottage with lake frontage is - 1.867513852723976
Dist for - 21 Lakefront Home with 90 Feet of Beach on  is - 0.16723017662153244
Dist for - 22 12 hours of Sebring 2015 Now available.  is - 2.2415279722624306
Dist for - 23  12 HOURS OF SEBRING 2015 Lodging, POOL  is - 2.341305343714389
Dist for - 24 Paradise on Lake Henry is - 2.350980389549641
Dist for - 25 RACEWEEK OPEN FOR MARCH14TH-20TH. BOOK I is - 2.4024846058745863
Dist for - 26 Rest and Relax at this Lake June Canal H is - 2.4137136067655165
Dist for - 27 Vacation Home on Beautiful Lake June is - 2.855113621669036
Dist for - 28 Spacious Lakefront Home Suited for Famil is - 3.0899549570788807
Dist for - 29 Lake Front Units on 3500 Acre Lake with  is - 3.1968086279685175
Dist for - 30 Spacious 2/1 Lake front on Lake Francis is - 3.853031623209993
Dist for - 31 Luxury Condo on Lake June in Lake Placid is - 4.063322037811134
Dist for - 32 On Beautiful Lake Francis is - 4.272605400889185
Dist for - 33 Lakehouse on Lake Francis is - 4.3022024460943
Dist for - 34 Steven and Louise Feller is - 4.403574090711126
Dist for - 35 Beautiful Lakehouse on Lake Francis is - 4.423557301583266
Dist for - 36 Lake Placid Lakefront is - 4.487153454616658
Dist for - 37 On Beautiful Lake Francis is - 4.514940849305256
Dist for - 38 Lakefront on Lake Francis is - 4.514952029669339
Dist for - 39 Lake Francis Home is - 4.580087323715115
Dist for - 40 Lake Francis Estate is - 4.60022361563561
Dist for - 41 Visit Paradise at 'sunshine rv resort'  is - 7.051312339930277 

Upvotes: 1

Views: 165

Answers (1)

Vivian River
Vivian River

Reputation: 32400

If you declare the sorting function with a function statement, it can appear anywhere is the closure where it is being used. If you declare the sorting function with a var statement, it needs to precede where it is being used. (Douglas Crockford recommends that the function declaration should always come first by convention, but that's a different discussion.)

Try this:

set.sort(sortDistanceLow);

function sortDistanceLow(a,b) { // instead of var sortDistanceLow = function(a,b)
    var aDist = geoLib.distance(a.lat,a.lng, sortOrigin.lat,sortOrigin.lng);
    var bDist = geoLib.distance(b.lat,b.lng, sortOrigin.lat,sortOrigin.lng);
    return (aDist-bDist);
}

If that doesn't work, I would suggest that you test your library with the points in question; your example looks simple and if you're getting the wrong result, I would test the library.

Update in response to OP's comment: I would say that if you have tested the code in both Firefox and Chrome and it works in one, but not the other, do the following.

(1) Make a copy of your web page that has this problem and take out everything from the web page that doesn't have to do with the bug. Then, reduce the web page to the absolute simplest thing you can make that will reproduce the problem. Can you get the wrong sorting behavior with an array of just two elements, for example?

(2) Once you've done (1), carefully review your simplified web page. If you still can't see what's causing the problem, post a new question to StackOverflow showing the code for the entire page (which should be very short!) and explain the expected behavior and the actual behavior.

(3) If you've done (2) and can't get an explanation for the discrepancy, then consider reporting it as a potential Chrome bug. In my experience, if a web page behaves different in Chrome and Firefox, it tends to be because there is something wrong with the page. Different browsers tend to behave differently when they encounter invalid HTML/CSS/JS.

Upvotes: 1

Related Questions