title: RoR: Sorting paginated products on an endless page
body: <p>I am using will_paginate gem to paginate products on an endless/infinite scroll page. The pagination works fine. </p>
  
  <p>But whyen I try to sort the products listed on that page, only the products that have been loaded upto that point get sorted, and the remaining products appear in random order after loading.</p>
  
  <p>Following is my controller, sorting is done based on <code>order</code> parameter :</p>
  
  <pre><code>def index
      @order = params[:order]                                                 
      @query = params[:tag]                                                   
      @taxon = params[:taxon]                                                 
      if @order.nil?
          @order = "relevance"                                                        
      end
      @products = queryTags(params, @taxon)                                       
      if ! @products.nil?
          if @order == "pricelow"
              @products = @products.sort_by{ |k| k['variants'][0]['Price'] }
          elsif @order == "pricehigh"
              @products = @products.sort_by{ |k| -k['variants'][0]['Price'] }
          elsif @order == "relevance"
              @products = @products.sort_by{ |k| -k['productRelevance'] }
          elsif @order == "pop"
              @product_group = @products.map{|product| product['id']}
              @result = Product.where(id: @product_group).descend_by_popularity
              @products = @result.map{|r| @products.find{|p| p['id'] == r['id']}}
          end
      end
      @productsProxy = Array.new
      if @products != nil
          @products.each do |p|
              tempProduct = ProductProxy.new(p)
              @productsProxy.push(tempProduct)
          end
      else
          @productProxy = []
      end
      if @order == "new"
          @productsProxy.sort!{ |a,b| b.productAvailableDateTs &lt;=&gt; a.productAvailableDateTs }
      end
      @productsProxy = @productsProxy.paginate(page: params[:page], per_page: 20)
      respond_with("Search Results") do |format|
          format.js
          format.html
          format.xml { render :xml =&gt; @productsProxy, :only =&gt; [:name, :permalink, :description, :mrp], :methods =&gt; [:designer, :taxons, :price] }
      end
  end
  </code></pre>
  
  <p>Why does the sorting happen only for the loaded products even when I am sorting the entire <code>@productsProxy</code> array.</p>
  
  <p>Thanks</p>