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.

3
  • 2
    What have you tried ? Share effort please. And maybe a duplicate of stackoverflow.com/questions/2784230/… Commented Dec 18, 2018 at 9:22
  • 2
    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, 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 minimal reproducible example. For more information, please see How to Ask and take the tour. Commented Dec 18, 2018 at 9:22
  • Maybe this answer 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; Commented Dec 18, 2018 at 10:36

3 Answers 3

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(' ')));

Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

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.
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

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.