sorting - jQuery Sort both ascending and descending (Not a table) -


I am trying to sort a list of results from price and rating. For simplicity, I have just added 2 results: Ryder Lodge and Olina Lodge I wrote a work that changes it in a way.

How can I reuse the same function to sort my results, such as by clicking on the 'Sort in Price' type, descending by ascending by clicking the ascending and dragging it between the two Does it?

  & lt; Div class = "tab-content" & gt; & Lt; Div id = "filter" & gt; & Lt; P & gt; & Lt; A href = "javascript: sort_by_rating ();" & Gt; Sort by rating & lt; / A & gt; & Lt; Br> & Lt; A href = "javascript: sort_by_price ();" & Gt; Sort by price & lt; / A & gt; & Lt; / P & gt; & Lt; / Div & gt; & Lt; Div id = "div_hotel_results" class = "tab-panel active" & gt; & Lt; Form ID = "hotel-form" & gt; ... & lt; / Form & gt; & Lt; Div class = "result-line" & gt; & Lt; H5 & gt; Riders Lodge & lt; / H5> & Lt; Span class = "Label label-information value" & gt; $ 103.64 & lt; / Span & gt; & Lt; Span class = "rating" data-rating = "3.0" & gt; 3.0 & lt; / Span & gt; & Lt; / Div & gt; & Lt; Div class = "result-line" & gt; & Lt; H5 & gt; Olena Lodge & lt; / H5> & Lt; Span class = "Label label-information value" & gt; $ 99.64 & lt; / Span & gt; & Lt; Span class = "rating" data-rating = "2.5" & gt; 2.5 & lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Script type = "text / javascript" & gt; Function sort_by_price () {var sorted_list = []; $ ('.price') Each (function (index) {sorted_list [index] = $ (this) .Parent () .Parent (); delete (); sort_list [index] ['cleaned_price'] = parseFloat ($ ( Replace it) .text (). (/ [0-9.] + / G, ""));}); Sorted_list.sort (if [a ['cleaned_price'] == b ['clean_price']) {return 0;} and if (a ['clean_price']> gt; b ['cleaned_price' ]) {Return1;} and {return -1;}}); After $ ('# Hotel Farm') (sorted_list) } & Lt; / Script & gt;    

Here's an example. I believe that you do not want the price just, but not the rating. I have created a variable that helps keep track of the current state of the sort.

HTML:

  & lt; Div class = "tab-content" & gt; & Lt; Div id = "filter" & gt; & Lt; P & gt; & Lt; A class = "sortbiriting" href = "#" & gt; Sort by rating & lt; / A & gt; & Lt; Br> & Lt; One class = "sortbippris" href = "#" & gt; Sort by price & lt; / A & gt; & Lt; / P & gt; & Lt; / Div & gt; & Lt; Div id = "div_hotel_results" class = "tab-panel active" & gt; & Lt; Form ID = "hotel-form" & gt; ... & lt; / Form & gt; & Lt; Div class = "result" & gt; & Lt; Div class = "result-line" & gt; & Lt; H5 & gt; Riders Lodge & lt; / H5> & Lt; Span class = "Label label-information value" & gt; $ 103.64 & lt; / Span & gt; & Lt; Span class = "rating" data-rating = "3.0" & gt; 3.0 & lt; / Span & gt; & Lt; / Div & gt; & Lt; Div class = "result-line" & gt; & Lt; H5 & gt; Olena Lodge & lt; / H5> & Lt; Span class = "Label label-information value" & gt; $ 99.64 & lt; / Span & gt; & Lt; Span class = "rating" data-rating = "2.5" & gt; 2.5 & lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt;   

Javascript:

  var ascending = false; $ ('Tab-content'). ('Click', 'sortepip', function () {var sorted = $ ('result-line'). Sort (function (A, B) {return (ascending == (convert to number ($ (A). Fund ('value') .html ()) convertToNumber ($) (b) .fund ('. Value'). Html ()))) 1: -1;}); Ascending = ascending? Wrong: True; $ ('Result') Html (sorted;)); Var ConvertFineNumber = Function (Value) {Return Pars Float (value location ('$', '')); }    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -