Solved

jQuery - Sort div's on click

Posted on 2010-09-18
6
1,466 Views
Last Modified: 2012-05-10
I am trying to make the boxes on this page sort: http://dws.commercegeneration.com:85/Shades.aspx

I think I almost have it, but I have it appending to each section. What other ways can I sort the divs by the price.

Right now I have only the "sort from low to high" - that is the only working link.

I think the code I have is a start.

Thanks,

Ryan


$(function () {
function reorder(sortby, direction){
    Array.prototype.sort.call($("div.shade-box"), function(a,b){
        var av = $(a).find("span."+sortby).text();
        var bv = $(b).find("span."+sortby).text();
        return direction=="ascending"? av-bv : bv-av;
    }).appendTo(".shade-listing");
}
	$('.sort-low-high').toggle(function() {
		reorder("shade-price-wrap","ascending");
/*
		$(this).html("Close box");
*/
	}, function() {
		reorder("shade-price-wrap","descending");
/*
		$(this).html(saveText);
*/
	});
});

Open in new window

0
Comment
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:DalHorinek
Comment Utility
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
Hey,

I am now working with the code below in the box. I made some changes to the code seen in the demo, on the click event:

http://qd9.co.uk/projects/jQuery-Plugins/sort/demo.html

My markup looks like this: http://pastie.org/1168658

I made it look for the <span> that holds the price and sort by that. Any suggestions?
var sortDiv = jQuery('.shade-box'),
    inverse = false;

$('.sort-low-high').click(function(){
    sortDiv.sort(function(a, b){
    	console.log(a);
        return $(a).next('.shade-price-wrap').text() > $(b).next('.shade-price-wrap').text() ? 1 : -1;
    });
});

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
Would it matter if I am sorting by INT and not alphabetical?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Accepted Solution

by:
DalHorinek earned 500 total points
Comment Utility
Actually now it sorts as strings, i guess.

In the demo, there is:

 a = $(a).text();
 b = $(b).text();
                       
  return (
          isNaN(a) || isNaN(b) ?
             a > b : +a > +b
          )
 
That means, if a or b are not numbers, they are sorted as strings, if they are numbers, then are sorted as numbers, because the +a > +b forces JS to handle them as numbers, but I guess it won't work with negative numbers.

I would do this:

return parseInt($(a).next('.shade-price-wrap').text(),10) > parseInt($(b).next('.shade-price-wrap').text(),10) ? 1 : -1;
    });
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
Thanks! let me try this. Sorry for the delay.

Ryan
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
Comment Utility
Good eye! Thank you for that.

Ryan
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now