Good evening,
I need to sort a list of HTML phrases by user name or by age, depending on which button is clicked. How can I do this?
Below is a clearer version of the code:
<html>
<head>
<title>JS</title>
</head>
<body>
<div id="master">
<button onClick="orderName()">Ordenar por nome</button>
<button onClick="orderIdade()">Ordenar por idade</button>
<br/>
<ul id="lista-usuarios">
<li>João é homem e possui 23 anos"</li>
<li>Ana é mulher e possui 19 anos"</li>
<li>Ricardo é homem e possui 32 anos"</li>
<li>Fernanda é mulher e possui 25 anos"</li>
</ul>
</div>
<script>
function orderName() {
var list = document.querySelector("#lista-usuarios");
console.log("Ordenado por nome");
}
function orderIdade() {
var list = document.querySelector("#lista-usuarios");
console.log("Ordenado por idade");
}
</script>
</body>
</html>
UPDATE:
If necessary, this is the full version .
Phrases are assembled from the users
array (as seen in the link above). I managed to make the filters work, so I opted to reduce the code, to make it easier.
Since I need the order to work along with the filters , I understood that it might be necessary to get the list from HTML
, to keep the filters in order (in my attempts, if I took from the array, you would have to clean the list, which would remove the filter).