Javascript 如何按名称对foursquare场馆进行排序(即排序

Javascript 如何按名称对foursquare场馆进行排序(即排序,javascript,json,sorting,foursquare,Javascript,Json,Sorting,Foursquare,我正在使用foursquare场馆API在我的web应用程序中填充一个选择菜单和列表。我想按名称的字母顺序对场馆进行排序 下面是来自foursquare API的JSON响应,它有一些地方: [ { "reasons": { "count": 1, "items": [ { "summary": "This spot is popular on foursquare", "type": "general", "reasonName": "globalInteractionReason" } ]

我正在使用foursquare场馆API在我的web应用程序中填充一个选择菜单和列表。我想按名称的字母顺序对场馆进行排序

下面是来自foursquare API的JSON响应,它有一些地方:

[ { "reasons": { "count": 1, "items": [ { "summary": "This spot is popular on foursquare", "type": "general", "reasonName": "globalInteractionReason" } ] }, "venue": { "id": "4c6ee03fb5a5236a74744b52", "name": "Peninsular Paper Dam", "contact": {}, "location": { "address": "1265 Leforge Rd", "crossStreet": "at Huron River Rd", "lat": 42.256628, "lng": -83.623933, "distance": 892, "postalCode": "48198", "city": "Ypsilanti", "state": "MI", "country": "United States", "cc": "US" }, "categories": [ { "id": "4bf58dd8d48988d165941735", "name": "Scenic Lookout", "pluralName": "Scenic Lookouts", "shortName": "Scenic Lookout", "icon": { "prefix": "https://foursquare.com/img/categories_v2/parks_outdoors/sceniclookout_", "suffix": ".png" }, "primary": true } ], "verified": false, "stats": { "checkinsCount": 31, "usersCount": 12, "tipCount": 0 }, "likes": { "count": 0, "groups": [] }, "specials": { "count": 0, "items": [] }, "photos": { "count": 2, "groups": [] } } }, { "reasons": { "count": 1, "items": [ { "summary": "This spot is popular on foursquare", "type": "general", "reasonName": "globalInteractionReason" } ] }, "venue": { "id": "4ba58202f964a520cb0d39e3", "name": "Benito's Pizza", "contact": { "phone": "7349610707", "formattedPhone": "(734) 961-0707" }, "location": { "address": "1088 N Huron River Dr", "lat": 42.256532, "lng": -83.629082, "distance": 1035, "postalCode": "48197", "city": "Ypsilanti", "state": "MI", "country": "United States", "cc": "US" }, "categories": [ { "id": "4bf58dd8d48988d1ca941735", "name": "Pizza Place", "pluralName": "Pizza Places", "shortName": "Pizza", "icon": { "prefix": "https://foursquare.com/img/categories_v2/food/pizza_", "suffix": ".png" }, "primary": true } ], "verified": false, "stats": { "checkinsCount": 50, "usersCount": 34, "tipCount": 0 }, "url": "http://www.benitospizza.com/", "likes": { "count": 0, "groups": [] }, "menu": { "type": "foodAndBeverage", "url": "https://foursquare.com/v/benitos-pizza/4ba58202f964a520cb0d39e3/menu", "mobileUrl": "https://foursquare.com/v/4ba58202f964a520cb0d39e3/device_menu" }, "specials": { "count": 0, "items": [] }, "photos": { "count": 0, "groups": [] } } } ]
我能够用以下代码解析此响应:

for (var i = 0; i < venues.length; i++) {
   name = venues[i]['venue']['name'];
   category = venues[i]['venue']['categories'][0]['name'];
   icon = venues[i]['venue']['categories'][0]['icon']['prefix'];
   icon = icon.slice(0, -1); // remove trailing "_" character
   icon = icon + venues[i]['venue']['categories'][0]['icon']['suffix'];
   address = venues[i]['venue']['location']['address'];
   city = venues[i]['venue']['location']['city'];
   state = venues[i]['venue']['location']['state'];
   distance_meters = venues[i]['venue']['location']['distance'];
   distance_miles = distance_meters / 1609.34;
   distance_miles = Math.round(distance_miles*100)/100;
   x = 1; // in the product use i for index below
   HTMLmarkupList += "<li><a href=\"#lister_" + x + "\"><img src=\"" + icon + "\" class=\"ui-li-thumb\" style=\"margin: 23px 10px\" onerror=\"ImgError(this);\">" + "<h3 style=\"margin-left: -40px\">" + name + "</h3><p style=\"margin-left: -40px\">" + category + "</p><p style=\"margin-left: -40px\">" + address + ", " + city + ", " + state + "</p><p style=\"margin-left: -40px\">" + distance_miles + " miles from you.</p></a></li>";

   HTMLmarkupSelect += "<option value\"" + i + "\">" + name + "</option>";
}
for(变量i=0;i”;
HTMLmarkupSelect+=“”+name+“”;
}
现在,select的值只是i,但由于我需要在数据库中存储其他变量以及名称,我可能会更新每个select选项中的值,以包括地址、城市、州等……我提到这一点是因为如果我只在select中使用名称,我可以构建一个名称数组并使用e javascript排序方法

有人能帮忙按名称的字母顺序排列场馆吗?谢谢。

信息:

例如:

venues.sort(function(a,b){
  if(a.venue.name == b.venue.name) return 0;
  return (a.venue.name < b.venue.name) ? -1 : 1;
});
场馆。排序(功能(a、b){
如果(a.venture.name==b.venture.name)返回0;
返回(a.场馆名称
数组
.sort()
函数可以传递一个比较器函数。该函数被传递给两个数组元素进行比较,并根据两个元素的正确顺序返回一个负、零或正的数字。@Pointy thx。尽管该页包含正确的信息。修复了我的函数。