Sort recently added records dynamically

1

I make this query in the database to list all the names of the registers ...

    $('.ordenar').click(function(){
                    db.transaction(function(tx){
                        tx.executeSql('SELECT nome FROM cartoes', [], function(tx,results){
                            for(i=0; i<results.rows.length; i++){
                                console.log(results.rows.item(i).nome);
                                //Aqui ele me retorna todos os nomes dos cadastros existentes, exemplo:
//nomecadastro1
//nomecadastro2
//e assim sucessivamente..
                            }

                        });
                    });
                });

These entries are inserted through an append, like this:

$(".listagem").append(
                                                            '<div class="row" id="corpo-cartoes">
                                                                <div class="col s12 m7" style="width: 100%;">
                                                                    <div class="card">
                                                                        <a href="cartao.html#${lastId}">
                                                                            <div class="card-image">
                                                                                <div class="gradient-cartao-lista"></div>
                                                                                <img src="${capa}">
                                                                                <span class="card-title"><h1>${data.card.empresa}</h1><p>${data.card.code}</p></span>
                                                                            </div>
                                                                        </a>
                                                                        <div class="card-action icone-meu-cartao">
                                                                            <a href="#"><i class="material-icons">star</i></a>
                                                                            <a href="#"><i class="material-icons">crop_free</i></a>
                                                                            <a href="#"><i class="material-icons">visibility</i></a>
                                                                            <a href="cadastro-cartao.html#${lastId}"><i class="material-icons btn-editar">edit</i></a>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>'
                                                        );  

Visually they look like this:

I first created A, then B ...

So it should be inverted, so B should get first than A.

So how could I sort them through the result?

I would like to sort them by the newly added, that is, from the last record to the first. How could I do this with JavaScript / Jquery?

    
asked by anonymous 14.12.2018 / 18:06

3 answers

2

flex and ul changes to toggleClass thus reversing the order of the list.

$( "button" ).click(function() {
  $( 'ul' ).toggleClass( "inverte" );
});
ul {
    display: flex;
    flex-direction: column;
}
.inverte {
    flex-direction: column-reverse;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<button>inverter</button>
<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
    <li>item 4</li>
</ul>
    
14.12.2018 / 18:31
1

If you want to do with query you can use the ORDER BY :

ORDER BY coluna DESC

Or you can sort on JavaScript by sort :

const rows = results.rows.sort((a,b) => return b.coluna - a.coluna);
    
14.12.2018 / 18:15
0

A hint of how you could sort would be using array.sort . To do this, you could map the results of your query to an array and perform a sort with a Custom sort function .

    
14.12.2018 / 18:16