Split in reverse order

0
var itens: "101|102|103|104";

var quebra = itens.split("|");
    for (var i = quebra.length - 1; i >= 0; i--) {
    var unidade = quebra[i];

$("#lista").append("<p>" +getValue(unidade, jsonTitles)+"</p>");
}

The result of this is as follows: 104 103 102 101

I would need it to be 101 102 103 104

    
asked by anonymous 24.03.2017 / 18:04

2 answers

4

Just use the reverse method.

var itens = "101|102|103|104";

var quebra = itens.split("|").reverse(); // inverte o array
for (var i = quebra.length - 1; i >= 0; i--) {
  var unidade = quebra[i];
  console.log(unidade);
}
    
24.03.2017 / 18:06
3

Add .sort(function (a, b) { return b - a; }) soon after itens.split("|") .

    
24.03.2017 / 18:07