2
I have to insert numbers and sort in ascending order to display the result. But I’m not getting the order right. Here’s what happens:
Insert yourself for example 76,3,15,35
returns ordered as follows: 15,3,35,76
.
// ordenar por ordem crescente os numeros inseridos.
const input1 = document.querySelector(".input1");
const btnConvert = document.querySelector(".btn-converter");
function sortNumbers(inputText) {
let numArray = inputText.split(",");
numArray.sort();
document.getElementById("h").innerText = (` ${ numArray} `);
console.log(numArray)
return numArray;
}
btnConvert.addEventListener('click', function () {
sortNumbers(input1.value);
});
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Modelo</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section class="container">
<h1>Exercícios</h1>
<label class="label"
>INSERIR NÚMEROS
<input type="text" class="input1" />
</label>
<button class="btn btn-converter">ORDENAR POR ORDEM CRESCENTE</button>
<h2 class="total" id="h"></h2>
</section>
<script src="./main.js"></script>
</body>
</html>
Okay, thanks :) However it would force me to always have two digits and not quite what I intended. Also, if for some strange reason I put 015,p. and, it would continue to order in the same way. But thanks for the tip anyway :)
– Ana Sofia
Ana, putting zero left is gambiarra, forget it. It is simpler to convert to number as it works for any values (as indicated in the answers below)
– hkotsubo
okay, thank you :)
– Ana Sofia
@hkotsubo, put zero left of hour with a digit is gambiarra too?
– user60252
@Leocaracciolo In the case of hours, putting zero on the left is a matter of formatting/presentation, it is completely different from what is being done here. Putting a zero to the left on the string just to force a correct ordering I consider gambiarra, especially when there is a simpler and cleaner solution (convert the strings to number). Different problems, different solutions :-)
– hkotsubo
@hkotsubo, your understanding, note that the question is: Sort string in ascending javascript order, I read STRING or need to update my glasses?
– user60252
@Leocaracciolo But we are ordering strings. Only the sorting criterion is to use the numeric value that the string represents, so I think it’s better to convert to number instead of adding zeros on the left. Because adding zeros is limited (if the string is
'76,03,123456,35'
, for example, it no longer works, you would have to add more zeros to each number to work - something like'000076,000003,123456,000035'
). And neither will I get the credit that'03'
is a string other than'3'
...– hkotsubo
@hkotsubo, you are right, turn into numbers to sort is the best solution, put zeros would give a tremendous turn, take the length of the largest element of the array, put as many zeros as necessary, sort and return to the initial elements for presentation. But it would be a good exercise right?
– user60252