Sort an array of objects

Thread: Sort an array of objects

1. Junior Member
Join Date
Dec 1969
Posts
4

Sort an array of objects

I use the follwing code to sort through an array of objects and put it in order by the State property.<BR><BR>function jsClientLocation(CompanyName,<BR> City,<BR> State,<BR> Zip,<BR> Country)<BR>{ <BR> this.CompanyName = CompanyName &#124&#124 ""<BR> this.City = City &#124&#124 ""<BR> this.State = State &#124&#124 ""<BR> this.Zip = Zip &#124&#124 ""<BR> this.Country = Country &#124&#124 ""<BR>}<BR><BR>function CompareState(Loc1, Loc2)<BR>{ return Loc1.State &#060; Loc2.State ? -1 :<BR> Loc1.State == Loc2.State ? 0 : 1; <BR>}<BR><BR>function SortLocationsByStateCity()<BR>{ arrLoc.sort(CompareState);<BR>}<BR><BR>arrLoc[1] = new jsClientLocation("Company A","SAN FERNANDO","CA","91340","US")<BR><BR>arrLoc[2] = new jsClientLocation("Company B","CLEVELAND","OH","44114","US")<BR><BR>arrLoc[3] = new jsClientLocation("Company C","RENTON","WA","98055","US")<BR><BR><BR><BR>Coul d anyone tell me how to take that a step further so that I can sort the array by State and then by City?<BR><BR>Thanks in advance.

2. Junior Member
Join Date
Dec 1969
Posts
4

RE: Sort an array of objects

Nevermind... I think I figured it out... In case anyone&#039;s interested:<BR><BR>function CompareStateCity(Loc1, Loc2)<BR>{ return Loc1.State &#060; Loc2.State ? -1 :<BR> Loc1.State &#062; Loc2.State ? 1 :<BR> Loc1.State == Loc2.State && Loc1.City &#060; Loc2.City ? -1 : <BR> Loc1.State == Loc2.State && Loc1.City &#062; Loc2.City ? 1 :<BR> Loc1.State == Loc2.State && Loc1.City == Loc2.City ? 0 : 1; <BR>}

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•