-1

I have an array given below, every element of this array consists of another array.

var array = [
  ['201', 'Tom', 'EES', 'California'],
  ['189', 'Charlie', 'EE', 'New Jersey'],
  ['245', 'Lisa', 'EEF', 'New Jersey'],
  ['743', 'Niall', 'EEC', 'Chicago'],
  ['653', 'Tim', 'EES', 'Miami'],
  ['333', 'Dev', 'EE', 'Washington'],
  ['333', 'Rhonda', 'EEC', 'Washington']
]

I want it to be sorted on the basis of 3rd value and in this order [EE,EES,EEC,EEF].

array should be:

[
  ['189', 'Charlie', 'EE', 'New Jersey'],
  ['333', 'Dev', 'EE', 'Washington'],
  ['201', 'Tom', 'EES', 'California'],
  ['653', 'Tim', 'EES', 'Miami'],
  ['743', 'Niall', 'EEC', 'Chicago'],
  ['333', 'Rhonda', 'EEC', 'Washington'],
  ['245', 'Lisa', 'EEF', 'New Jersey']
]

Note :- Original array will be having n no of elements, all i want is that first EE elements should come , then EES, EEC and then EEF

Thanks in advance.

amanbisht39
  • 73
  • 1
  • 7

1 Answers1

1

//this is my code! You can use array.sort(...) to do this


var array=[['201','Tom','EES','California'],['189','Charlie','EE','New Jersey'],
['245','Lisa','EEF','New Jersey'],['743','Niall','EEC','Chicago'],['653','Tim','EES','Miami'],
['333','Dev','EE','Washington'],['333','Rhonda','EEC','Washington']];


let _result = array.sort(
    function(a, b){
        if (a[2].toLowerCase() < b[2].toLowerCase()) return -1;
            if (a[2].toLowerCase() > b[2].toLowerCase()) return 1;
            return 0;
    }

)

console.log(_result);
quangdang
  • 116
  • 7