Javascript数组排序问题

时间:2014-11-04 20:25:51

标签: javascript arrays sorting

在Javascript中排序对象数组有问题。对具有lat,lng并尝试按特定点距离排序的对象数组进行排序。对象看起来像这样(不是一个完整的对象,但你明白了)

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

使用以下方式排序:

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);
}

排序的结果有2个项目无序,对于我的生活,我无法弄清楚原因。看看索引0和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 

1 个答案:

答案 0 :(得分:1)

如果使用函数语句声明排序函数,它可以出现在使用它的闭包的任何地方。如果使用var语句声明排序函数,则需要在它使用的位置之前。 (道格拉斯·克罗克福德建议,函数声明应该始终按惯例排在第一位,但这是一个不同的讨论。)

试试这个:

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);
}

如果这不起作用,我建议你用有问题的点测试你的图书馆;您的示例看起来很简单,如果您得到错误的结果,我会测试该库。

更新以回应OP的评论:我想说如果你在Firefox和Chrome中测试了代码并且它在一个中工作,而在另一个中工作,则执行以下操作

(1)制作出现此问题的网页副本,并从不与该错误有关的网页中取出所有。然后,将网页缩小为可以重现问题的绝对最简单的方法。你能用两个元素的数组得到错误的排序行为吗,例如?

(2)完成后(1),仔细阅读简化的网页。如果您仍然无法查看导致问题的原因,请向StackOverflow发布一个新问题,显示整个页面的代码(应该非常简短!)并解释预期的行为和实际行为。< / p>

(3)如果您已完成(2)但无法获得有关差异的说明,请考虑将其报告为潜在的Chrome错误。根据我的经验,如果网页在Chrome和Firefox中表现不同,则往往是因为网页出现了问题。当遇到无效的HTML / CSS / JS时,不同的浏览器往往表现得不同。