Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
¿Cómo puedo hacer un botón de clic cuando hago clic en hacer una matriz de ordenación (ab) y luego hago clic nuevamente para ordenar (ba)?

Aquí está mi pregunta ¿Cómo puedo hacer un botón de clic cuando hago clic en hacer array.sort(xy) y luego hago clic de nuevo para hacer array.sort(yx)?

Ya tengo la función, pero estoy confundido sobre cómo hacerlo...

permítanme aclarar mi pregunta: ¿cuándo hago clic en el botón ordeno la matriz de mayor a menor y luego me gustaría presionar el mismo botón para ordenar la matriz de menor a mayor?

aquí está mi código:

 $(".lower").click(() => { colorsValueMenu("highest", "spot", "gainers", "losers", "lower") highest = arrayCoinsD.sort(function (a, b) { return b.quote.USD.percent_change_24h - a.quote.USD.percent_change_24h }) createCoinDiv(arrayCoinsD, arrayCoinsI) $(".lower").click(() => { colorsValueMenu("lower", "spot", "gainers", "losers", "highest") lower = arrayCoinsD.sort(function (a, b) { return a.quote.USD.percent_change_24h - b.quote.USD.percent_change_24h }) createCoinDiv(arrayCoinsD, arrayCoinsI) }) })

funciona, pero solo la primera y la segunda vez... quiero que sea como una acción de alternar clase para cada clic

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Debe hacer su código DRY (Don't Repeat Yourself)

Puede crear una flag y cambiar las operaciones según la flag como:

 let flag = "high"; $(".lower").click(() => { // Highest colorsValueMenu( flag === "high" ? "highest" : "lower", "spot", "gainers", "losers", "lower" ); highest = arrayCoinsD.sort(function (a, b) { return flag === "high" ? b.quote.USD.percent_change_24h - a.quote.USD.percent_change_24h : a.quote.USD.percent_change_24h - b.quote.USD.percent_change_24h; }); createCoinDiv(arrayCoinsD, arrayCoinsI); flag = flag === "high" ? "low" : "high"; });
about 4 years ago · Juan Pablo Isaza Report

0

Lo que está haciendo en este momento es crear un detector de eventos en #lower. Esto hará la ordenación (ab) como usted pide, pero dentro de ese detector de eventos está creando otro detector de eventos, que no funcionará. Lo que puede hacer en su lugar es crear una bandera como alguien en los comentarios sugeridos. Un ejemplo sería:

 flag = false; ("#lower").click(() => { flag = !flag; if (flag) { //do this thing; } else { //do this other thing; } }

De esta manera, el comportamiento se alterna cada vez que hace clic en él. Luego puede usar eso para ordenar de diferentes maneras.

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el método de toggle para ascender y cuando haga clic nuevamente en descender de esta manera:

 $(".lower").toggle(function(){ // Ascending code or function }, function (){ // Descending code or function })

Consulte la referencia oficial aquí: https://api.jquery.com/toggle-event/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!