4

How can I sort an array of arrays by the second element which is also an array that contains only one element?

For example, the following array

array = [
    ["text", ["bcc"], [2]],
    ["text", ["cdd"], [3]],
    ["text", ["aff"], [1]],
    ["text", ["zaa"], [5]],
    ["text", ["d11"], [4]]
];

Should be sorted as follows:

sorted_array = [
    ["text", ["aff"], [1]],
    ["text", ["bcc"], [2]],
    ["text", ["cdd"], [3]],
    ["text", ["d11"], [4]],
    ["text", ["zaa"], [5]]
];
5
  • I see 3 level of arrays here. if it is single value why child 'bcc' is in array. it is possible it may have more values? Commented Feb 15, 2017 at 13:57
  • 1
    would you like to sort with ['bcc'] or with the number [2]? Commented Feb 15, 2017 at 14:04
  • @NinaScholz I need the array to be sorted in alphabetical order depending on the second element from each array Commented Feb 15, 2017 at 14:06
  • arrays are zero based, what is your second element? Commented Feb 15, 2017 at 14:07
  • 1
    ['bcc'] for example Commented Feb 15, 2017 at 14:09

6 Answers 6

2

You should use .sort() method which accepts a callback function.

Also, you have to use .localeCompare method in order to compare two strings.

array = [
    ["text", ["bcc"], [1]],
    ["text", ["cdd"], [1]],
    ["text", ["aff"], [1]],
    ["text", ["zaa"], [1]],
    ["text", ["d11"], [1]]
];
var sortedArray=array.sort(callback);
function callback(a,b){
  return a[1][0].localeCompare(b[1][0]);
}
console.log(sortedArray);

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

Comments

2

You can use sort() method like this.

var array = [
    ["text", ["bcc"], [1]],
    ["text", ["cdd"], [1]],
    ["text", ["aff"], [1]],
    ["text", ["zaa"], [1]],
    ["text", ["d11"], [1]]
];

var result = array.sort((a, b) => a[1][0].localeCompare(b[1][0]))
console.log(result)

1 Comment

what if you wanna sort based on all the element of the array, is there a shortcut?
2

You can achieve it by passing compared child level array in array sort function

array = [
    ["text", ["bcc"], [1]],
    ["text", ["cdd"], [1]],
    ["text", ["aff"], [1]],
    ["text", ["zaa"], [1]],
    ["text", ["d11"], [1]]
];

function Comparator(a, b) {
   if (a[1] < b[1]) return -1;
   if (a[1] > b[1]) return 1;
   return 0;
}

array = array.sort(Comparator);
console.log(array);

Hope it helps

Comments

2

You could sort the nested elements with String#localeCompare.

var array = [["text", ["bcc"], [2]], ["text", ["cdd"], [3]], ["text", ["aff"], [1]], ["text", ["zaa"], [5]], ["text", ["d11"], [4]]];

array.sort(function (a, b) {
    return a[1][0].localeCompare(b[1][0]);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

(Only for modern JavaScript engines)

array.sort(([,[a]], [,[b]]) => a.localeCompare(b))

Comments

0

You can do:

array.sort(function(a, b) { 
    if (a[1][0] > b[1][0])
        return 1;
    else if (a[1][0] < b[1][0])
        return -1;
    return 0; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.