Created
June 26, 2013 20:48
-
-
Save n3wc/5871513 to your computer and use it in GitHub Desktop.
sort sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sort_by = function(field, reverse, primer) { | |
var key = function(x) { | |
return primer ? primer(x[field]) : x[field] | |
}; | |
return function(a, b) { | |
var A = key(a), B = key(b); | |
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse]; | |
} | |
}; | |
var sortNested_by = function(nestedfield, field, reverse, primer) { | |
var key = function(x) { | |
return primer ? primer(x[nestedfield][field]) : x[nestedfield][field] | |
}; | |
return function(a, b) { | |
var A = key(a), B = key(b); | |
return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse]; | |
} | |
}; | |
[{"UserListings":{"ListingID":"69798345","SiteID":"150000058","Address":"11125125","City":null,"State":"FL","Zip":null,"ListingStatus":"Active","Active":"1","ListingType":"Farm","ListingAgentContactID":"18808903","ListingTypeID":"106","ListingStatusID":"100","MLSNumber":"11125125","ListingAgent":"Jezzi, Hamby","Price":"100000.0000","ListingDate":"2013-01-24T00:00:00-05:00","IsEnhanced":"1","ListingSourceID":"0","ListingSourceTypeID":"4","ListingSource":"City (Pending)","ListingImageURL":null,"ListingLink":"qaagent1.homesconnect.com/Listing/69798345-150000058/"}},{"UserListings":{"ListingID":"64331470","SiteID":"150000058","Address":"3094 Malaga St","City":"San Diego","State":"CA","Zip":"92106","ListingStatus":"Active","Active":"1","ListingType":"Single Family","ListingAgentContactID":"18808903","ListingTypeID":"100","ListingStatusID":"100","StreetName":"Malaga St","MLSNumber":"120039476","ListingAgent":"Jezzi, Hamby","Price":"619900.0000","ListingDate":"2012-08-03T00:00:00-04:00","MetroAreaID":"231","IsEnhanced":"0","ListingSourceID":"10000","ListingSourceTypeID":"4","ListingSource":"eN QA Group, Test Feed","ListingImageURL":null,"ListingLink":"qaagent1.homesconnect.com/Listing/64331470-150000058/"}},{"UserListings":{"ListingID":"76861092","SiteID":"150000058","Address":"23 My Street","City":"Your Hometwon","State":"FL","Zip":"33333","ListingStatus":"Active","Active":"1","ListingExpirationDate":"2013-11-15T00:00:00-05:00","ListingType":"Private Island","ListingAgentContactID":"18808903","ListingTypeID":"109","ListingStatusID":"100","StreetName":"My Street","MLSNumber":null,"ListingAgent":"Jezzi, Hamby","Seller":"testing, test0417","SellerContactID":"21863054","Price":"850000.0000","ListingDate":"2013-05-15T16:02:00-04:00","IsEnhanced":"1","ListingSourceID":"0","ListingSourceTypeID":"4","ListingSource":"City (Pending)","ListingImageURL":null,"ListingLink":"qaagent1.homesconnect.com/Listing/76861092-150000058/"}}] | |
if (sort == 'priceLH') { | |
ListingArray.sort(sortNested_by("UserListings", 'Price', true, parseInt)); | |
} else if (sort == 'priceHL') { | |
ListingArray.sort(sortNested_by("UserListings", 'Price', false, parseInt)); | |
} else if (sort == 'newold') { | |
ListingArray.sort(sortNested_by("UserListings", "ListingDate", false, parseInt)); | |
} else if (sort == 'oldnew') { | |
ListingArray.sort(sortNested_by("UserListings", "ListingDate", false, parseInt)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment