2

I have an array of JavaScript that holds students and their lesson appointments;

tablo[0] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[1] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[6] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[7] = ['Ashley','Class4C','2018.11.12','13:50'];
tablo[8] = ['John','Class3A','2018.01.01','18:50'];
tablo[9] = ['John','Class3A','2018.01.01','10:50'];
tablo[10] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[11] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[12] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[13] = ['John','Class3A','2018.01.01','07:50'];
tablo[14] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[15] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','10:50'];

I want to sort this array first by class names then by student names, then by date and finally by hour. The sorted array must be like that;

tablo[0] = ['John','Class3A','2018.01.01','07:50'];
tablo[1] = ['John','Class3A','2018.01.01','10:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['John','Class3A','2018.01.01','18:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[6] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[7] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[8] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[9] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[10] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[11] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[12] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[13] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[14] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[15] = ['Ashley','Class4C','2018.11.12','10:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','13:50'];

There are many ways to sort by 2 criteria but, i can't overcome to sort by 4 criteria like the example above.

Rose_The_Only
  • 238
  • 9
  • 21
  • 2
    What have you tried ? Share effort please. And maybe a duplicate of https://stackoverflow.com/questions/2784230/how-do-you-sort-an-array-on-multiple-columns – Alexis Dec 18 '18 at 09:22
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 18 '18 at 09:22
  • Maybe [this answer](https://stackoverflow.com/a/53758006/1641941) can help. Since you have array of arrays you can compare a string like so: `const compareClassName = (direction) => (a, b) => a[1].localeCompare(b[1]) * direction;` – HMR Dec 18 '18 at 10:36

3 Answers3

3

Try this

tablo.sort( (a,b)=> a[1].localeCompare(b[1]) || a[0].localeCompare(b[0]) ||
                    a[2].localeCompare(b[2]) || a[3].localeCompare(b[3])    );

tablo = [];

tablo[0] = ['Molly','Class3A','2018.12.18','13:50'];
tablo[1] = ['Ashley','Class4C','2018.10.14','14:50'];
tablo[2] = ['John','Class3A','2018.01.01','13:50'];
tablo[3] = ['Molly','Class3A','2018.11.20','13:50'];
tablo[4] = ['John','Class3A','2018.12.18','15:50'];
tablo[5] = ['Molly','Class3A','2018.09.11','13:50'];
tablo[6] = ['Ashley','Class4C','2018.10.14','15:50'];
tablo[7] = ['Ashley','Class4C','2018.11.12','13:50'];
tablo[8] = ['John','Class3A','2018.01.01','18:50'];
tablo[9] = ['John','Class3A','2018.01.01','10:50'];
tablo[10] = ['Molly','Class3A','2018.12.31','13:50'];
tablo[11] = ['Ashley','Class4C','2018.10.14','08:50'];
tablo[12] = ['Molly','Class3A','2018.05.07','13:50'];
tablo[13] = ['John','Class3A','2018.01.01','07:50'];
tablo[14] = ['Molly','Class3A','2018.09.11','12:50'];
tablo[15] = ['Molly','Class3A','2018.09.11','15:50'];
tablo[16] = ['Ashley','Class4C','2018.11.12','10:50'];

tablo.sort( (a,b)=> a[1].localeCompare(b[1]) || a[0].localeCompare(b[0]) ||
                    a[2].localeCompare(b[2]) || a[3].localeCompare(b[3]) );

console.log(tablo.map(x=>x.join(' ')));
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

You can use string#localeCompare to sort your data.

let tablo = [[ "Molly", "Class3A", "2018.12.18", "13:50" ], [ "Ashley", "Class4C", "2018.10.14", "14:50" ], [ "John", "Class3A", "2018.01.01", "13:50" ], [ "Molly", "Class3A", "2018.11.20", "13:50" ], [ "John", "Class3A", "2018.12.18", "15:50" ], [ "Molly", "Class3A", "2018.09.11", "13:50" ], [ "Ashley", "Class4C", "2018.10.14", "15:50" ], [ "Ashley", "Class4C", "2018.11.12", "13:50" ], [ "John", "Class3A", "2018.01.01", "18:50" ], [ "John", "Class3A", "2018.01.01", "10:50" ], [ "Molly", "Class3A", "2018.12.31", "13:50" ], [ "Ashley", "Class4C", "2018.10.14", "08:50" ], [ "Molly", "Class3A", "2018.05.07", "13:50" ], [ "John", "Class3A", "2018.01.01", "07:50" ], [ "Molly", "Class3A", "2018.09.11", "12:50" ], [ "Molly", "Class3A", "2018.09.11", "15:50" ], [ "Ashley", "Class4C","2018.11.12", "10:50" ] ]
tablo.sort((a,b) => a[1].localeCompare(b[1],undefined, {numeric:true}) || a[0].localeCompare(b[0]) || a[2].localeCompare(b[2]) || a[3].localeCompare(b[3]) );
console.log(tablo);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • Best to have a function implement one thing only, if you were to name your sort function it be named `sortByClassNameThenByName`. This will reduce re usability and composability of your functions. – HMR Dec 18 '18 at 10:33
1

You can try this.

tablo.sort(function (a, b) {
   var res = a[1].localeCompare(b[1]); // Compare by classname
   if(res == 0) {   //Classnames are same, so comapre by student name
       res = a[0].localeCompare(b[0]); //Compare by student name
       if(res == 0){ //Student names are same, so comapre by date
           res = a[2].localeCompare(b[2]); //Compare by date TODO: Use some date parsing library like "momentjs" if required
           if(res == 0){ //Date is same, so comapre by hour
               res = a[3].localeCompare(b[3])
           }
       }
   }
   return res

});
Shiva
  • 543
  • 1
  • 6
  • 20