我有一个数组
var a=[['test':'1','test1':'2','test2':{'test3':'3','test4':'4'}],['test':'2','test1':'2','test2':{'test3':'1','test4':'2'}]];
我可以使用test和test 1字段对数组进行排序。但我不知道使用test3或test4进行排序。如何对这个数组进行排序。
CODE
[{"HotelId":18,"HotelName":"Trader","HotelAlias":"trader-hotel","HotelStreet":null,"Address":"No.18","HotelDescription":"Near to Shwedagone Pagoda","HotelLat":16.819910049438,"HotelLng":96.130912780762,"CityId":33,"RegionId":1,"Breakfast":1,"Lunch":0,"Dinner":0,"Snack":0,"Wifi":1,"CreatedOn":"2016-08-24 14:09:57","city":{"CityId":33,"CityName":"Bahan","RegionId":1,"CityPostCode":"11210","CityDescription":null,"region":{"RegionId":1,"RegionName":"Yangon","ISO":"MM-06","RegionDescription":"Old Capital of Myanmar"}},"room_count":null,"room_lock_count":null},{"HotelId":17,"HotelName":"Yuzana","HotelAlias":"yuzana-hotel","HotelStreet":null,"Address":"No.18","HotelDescription":"Near to Shwedagone Pagoda","HotelLat":16.819910049438,"HotelLng":96.130912780762,"CityId":33,"RegionId":1,"Breakfast":1,"Lunch":0,"Dinner":0,"Snack":0,"Wifi":1,"CreatedOn":"2016-08-22 17:47:42","city":{"CityId":33,"CityName":"Bahan","RegionId":1,"CityPostCode":"11210","CityDescription":null,"region":{"RegionId":1,"RegionName":"Yangon","ISO":"MM-06","RegionDescription":"Old Capital of Myanmar"}},"room_count":null,"room_lock_count":null},{"HotelId":16,"HotelName":"Strand","HotelAlias":"strand-hotel","HotelStreet":null,"Address":"No.18","HotelDescription":"The river view","HotelLat":16.819910049438,"HotelLng":96.130912780762,"CityId":12,"RegionId":1,"Breakfast":1,"Lunch":0,"Dinner":0,"Snack":1,"Wifi":1,"CreatedOn":"2016-08-22 13:57:27","city":{"CityId":12,"CityName":"Kyauktada","RegionId":1,"CityPostCode":null,"CityDescription":null,"region":{"RegionId":1,"RegionName":"Yangon","ISO":"MM-06","RegionDescription":"Old Capital of Myanmar"}},"room_count":null,"room_lock_count":null},{"HotelId":14,"HotelName":"Novotal","HotelAlias":"novotal-hotel","HotelStreet":null,"Address":"Kamayut","HotelDescription":"The best hotel","HotelLat":16.819910049438,"HotelLng":96.130912780762,"CityId":11,"RegionId":1,"Breakfast":1,"Lunch":0,"Dinner":0,"Snack":1,"Wifi":1,"CreatedOn":"2016-08-22 11:48:35","city":{"CityId":11,"CityName":"Kamayut","RegionId":1,"CityPostCode":null,"CityDescription":null,"region":{"RegionId":1,"RegionName":"Yangon","ISO":"MM-06","RegionDescription":"Old Capital of Myanmar"}},"room_count":{"HotelId":14,"qty":7},"room_lock_count":{"HotelId":14,"qty":7}}]
答案 0 :(得分:1)
这应该按test3
排序。
a.sort(function (item1, item2) {
if (item1.test2.test3 < item2.test2.test3) return -1;
if (item1.test2.test3 > item2.test2.test3) return 1;
return 0;
});