Home > other >  How to sort dates after picking them from input .js
How to sort dates after picking them from input .js

Time:12-16

Does anybody know how to properly sort timeline and dates descanding?

    <script>
        var productNames = ['krzesło', 'stół', 'taboret', 'ławka', 'figurki', 'szafka', 'blat'];
        var clientNames = ['Stolmach','Semoniera','Maluta', 'Budimex', 'Google', 'Kummer'];
        var legalForms = ['sp. z.o.o', 'Spółka Jawna', 's.c', 'S.A', 'Spółka partnerska', 'Spółka akcyjna', 'Spółka komandytowo-akcyjna', 'Comapny Limited by shares','Company Limited by guarantee','Unlimited company'];
        let invoices = [];
        var taxes = [23, 23, 23, 23, 8, 8, 5, 0];
        for(var i=0; i<200; i  ){
            invoices[i]=[];
            invoices[i][0] = new Date(2000   Math.floor( Math.random() * 21), 1   Math.floor( Math.random() * 12), 1   Math.floor( Math.random() * 20));
            invoices[i][1] = clientNames[Math.floor( Math.random() * 6)];
            invoices[i][2] = legalForms[Math.floor( Math.random() * 10)];
            invoices[i][3] = productNames[Math.floor( Math.random() * 7)];
            invoices[i][4] = taxes[Math.floor( Math.random() * 8)];
            invoices[i][5] = Math.floor(Math.random() * 1000) / 100;
            invoices[i][6] = Math.floor(invoices[i][5] * invoices[i][4]);
            invoices[i][7] = Math.floor(Math.random() * 1001);  
            invoices[i][8] = productNames[Math.floor( Math.random() * 7)];
            invoices[i][9] = productNames[Math.floor( Math.random() * 7)];       
        }

        function pod(){
            let data1 = document.getElementById('d1').value;
            let data2 = document.getElementById('d2').value;
            if(data1>data2){
                alert("Podane daty są niepoprawne");
                return 0;
            }else{
                for(var i=0; i<150; i  )
                if(data2 > data1){
                    console.log("hej");
                    var s = "<a class='k'>ID: </a>" invoices[i][7] "</br><a class='k'>Data: </a>" invoices[i][0] "</br><a class='k'>Nazwa klienta:</a>" invoices[i][1] "</br><a class='k'>Typ działalności:</a>" invoices[i][2] "</br><a class='k'>Produkty: </a>" invoices[i][3] ',' invoices[i][8] ',' invoices[i][9] "</br><a class='k'>Podatek: </a>" invoices[i][4] "</br><a class='k'>Cena po odliczeniu podatku:</a>" invoices[i][5] "</br><a class='k'>Podatek w cenie: </a>" invoices[i][6] "</br><hr></br>";
                    document.getElementById("generate").innerHTML  = s;
        
                }
            }
        

}
        

    </script>
    <body>
        <h1>Wykaz faktur</h1>
        <div id="przyciski">
            <form method="get">
                <a>Data od: </a>
                <input required type="date" id="d1">
                <a>Data do: </a>
                <input required type="date" id="d2">
                <input type="button" value="generate" onclick="pod()">
            </form>
        </div>
            <div id="generate">
            </div>
    </body>

We tried to make it without sorting just in proper timeline (ex. 2007-2020) but it doesnt work too and we wonder if its possible to even make it run like that

CodePudding user response:

It's pretty straightforward. It should just be

invoices.sort(function(a,b) {
  return a[0] > b[0];
});
  • Related