views:

25

answers:

1

I've completed writing a purpose-specific plugin and there are a few spots where I have duplicate code and due to the structure of the function, I'm not quite sure how to cleanly get rid of repetitious code.

You can see the code here:

http://jsbin.com/aboca3/8/edit

I am specifically referring to the 4 switch statements and wondering how I can consolidate them in to a function in a way that makes sense, and more importantly, actually works.

Last but certainly not least, there may be more efficient ways to go about performing the functions I'm performing so if anyone has any input on that, I'd be very grateful.

+1  A: 

Well those switch statements repeat almost the exact same code. Why not replace them with an object that you use to look up which sort function to call?

 var sorts = {
   'alpha': sortAlpha, 'em': sortEm, 'rel': sortRel
 };

then you can just call

 $('whatever')
   .sort(sorts[o.startingsort])
Pointy
Can you clarify a little?
Greg-J
Got it. Thanks Pointy!
Greg-J
There is a site (other than stackoverflow) where you post your code and other people try to improve upon it, do you know what that site was called?
Greg-J
I know what you mean but I've never used it; could it be "pastebin"?
Pointy