Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts

    Ordering array dependent on price

    Good afternoon,

    I have some properties on a real estate website. They all have their own div which has a class of propertyBox. I've selected them all by looping through and saving the value with .html().

    The price is within this array of strings and I need to order the array via price. Either low/high or high/low.

    How can I sort the array depending on price? I'm not sure how to select only the price then re-order them.

    Here is my code up to now. Been playing around a bit.

    Code:
    $(function(){
    
    	function getPrices(properties)
    	{
    		var prices = $('.propertyBox > span').text();
    		prices = $.map(prices, function(value, i){
    			return value.replace(/[^0-9]/g, '');
    		});
    		console.log(prices);
    
    	}
    	
    	var properties = new Array();
    	
    	$('.propertyBox').each(function(){
    		properties.push($(this).html());
    	});
    
    	console.log(properties);
    	
    	
    
    	$('#propertyForm select').change(function(){
    
    		switch($(this).val())
    		{
    			case 'Price Low/High':
    				getPrices(properties);
    			break;
    
    			case 'Price High/Low':
    
    			break;
    
    			default: /*reset properties back to initial state*/;
    		}
    
    		console.log($(this).val());
    	});
    
    
    });
    Thank you for your time.

    Kind regards,

    LC.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    IMHO I think your doing this all wrong.
    I'd set up the array (low-to-high prices) in PHP when you query the DB and set up a second array (high-to-low) with PHP array_reverse().
    Then make your HTML based on the <select> value.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Good evening sunfighter,

    Thanks for your reply.

    There is not database from which I am pulling these properties from.

    They have properties displayed on the homepage which are manually added by the website owner which I have hard coded.

    Is this possible to do with just jQuery?

    Thanks again,

    Kind regards,

    LC.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,076
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Quote Originally Posted by LearningCoder View Post
    They have properties displayed on the homepage which are manually added by the website owner which I have hard coded.
    Are you using a form for entree of div items? You should enter them into a DB at this point and into the array for ordering the web site.

    If you need help with this post the form and the coding for the <div class="propertyBox">

    I thought I would add this:
    In real estate listings you need more then ordering by price. People look for a number of things when buying a house. The number of bath rooms or bed rooms comes to mind. You will save yourself a lot of head aches setting up a DB with a many-to-many relationship I believe.
    Think about it. In my case I need to find a house with two full baths and three bed rooms in the $75,000 to $125,000 range on 1/2 acre. See what I mean?
    Last edited by sunfighter; 12-07-2013 at 03:28 AM. Reason: added talk about DB tables
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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