cootetom Posted April 28, 2006 Share Posted April 28, 2006 Hey all, i'm trying to sort an array of objects. I want to sort them into order of an object property named 'vendor', which is a string. I'm using a sorting algorithm that i use in java and have never tried in javascript so i don't know if it works. The few questions i have are;- Does javascript allow recursive calls?- If i compare strings like (array.vendor <= anotherstring) will this work?- To swap i have created a temporary object. Can i simply say tempObject = array ?Anyways you can view my code at the link below so you can see why i'm asking these questions. At the moment it does nothing when i call this function so i'm assuming some error somewhere. By the way i have an array of objects called 'inspectedCars' which includes a property called 'vendor' globally. I'm calling this function with 0 and inspectedCars.length so that it will sort the whole array, if i ever getting it working.sortList Function CodeAny help appreciated.- Tom Link to comment Share on other sites More sharing options...
Webworldx Posted April 29, 2006 Share Posted April 29, 2006 Javascript 1.6 should have the array.sort() function built in :)Yes, you can use recursion in javascript. Not quite sure what your other two questions are asking though. Link to comment Share on other sites More sharing options...
raimo Posted April 29, 2006 Share Posted April 29, 2006 so i'm assuming some error somewhere. <{POST_SNAPBACK}> You can't use break outside of loops:if (a >= b) { break; }if (inspectedCars.length < 2) { break; } this way You can do:if (a >= b || inspectedCars.length < 2) return false; Fixed and cleaned it a bit, I'm not sure how it's working,but anyway there is no errors or warnings anymore. <script type="text/javascript"><!--function sortList(a, b){if (a >= b || inspectedCars.length < 2) return false; var pivot = inspectedCars[b].vendor; var left = a; var right = b-1;var tempCar = new Object(); while (left <= right){ while (left <= right && inspectedCars[left].vendor <= pivot) left += 1; while (right >= left && inspectedCars[right].vendor >= pivot) right -= 1; if (left < right){ tempCar = inspectedCars[left]; inspectedCars[left] = inspectedCars[right]; inspectedCars[right] = tempCar; } } tempCar = inspectedCars[left];inspectedCars[left] = inspectedCars[b];inspectedCars[b] = tempCar; sortList(a,left - 1);sortList(left + 1, b);return "something"; //only removing FireBug warning here;)}//--></script> Link to comment Share on other sites More sharing options...
cootetom Posted April 29, 2006 Author Share Posted April 29, 2006 Thats great I can see where I was making mistakes so thanks.The built in array.sort() method won't work here as far as I know becuase i want to sort the array by a property of the object in it. The array.sort() will convert the object to a value so will take into account all other properties as well. Atleast thats my understanding of it.I'm learning javascript at the moment and am used to java but it's going well. I like some of the advantages to the looseness of the language but sometimes it makes it hard to code initially.- Tom Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now