2

Does anybody know how to sort dates descending?

    <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 are wondering if its possible to even make it run like that

Kevin Hernandez
  • 1,270
  • 2
  • 19
  • 41
  • Maybe try to convert the dates to epoch then you should be able to do simple math with it. – Kevin Hernandez Dec 15 '21 at 18:40
  • It's always a good idea to try to make sure your datetime format is ISO8601 (the format you get from (new Date()).toJSON() ). That format and handled can be sorted with no problem.. At least once in your coding carrier you will curse people writing the name of a month in swedish, finnish, Portuguese or god forbid arabic/hebrew – Griffin Dec 15 '21 at 18:52
  • Probably a duplicate of [*How to sort an object array by date property?*](https://stackoverflow.com/questions/10123953/how-to-sort-an-object-array-by-date-property/10124053?r=SearchResults&s=2|138.9724#10124053) – RobG Dec 16 '21 at 03:08

1 Answers1

0

It's pretty straightforward. It should just be

invoices.sort(function(a,b) {
  return a[0] > b[0];
});
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • That will treat `a == b` and `a < b` as equivalent. If *a* and *b* are either Dates or ISO 8601 strings then `(a, b) => a - b` does the job, see [*How to sort an object array by date property?*](https://stackoverflow.com/questions/10123953/how-to-sort-an-object-array-by-date-property) – RobG Dec 16 '21 at 00:12