Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,693 questions

57,001 answers

573 users

How to sort multidimensional array with key and value in JavaScript

3 Answers

0 votes
var arr = [
  ["key1", 4],
  ["key2", 2],
  ["key3", 1],
  ["key4", 5],
  ["key5", 3]
];
    
arr = arr.sort(function(a, b) {
  return a[1] - b[1];
})    
    
document.write(JSON.stringify(arr));
    

/*
run:

[["key3",1],["key2",2],["key5",3],["key1",4],["key4",5]]
  
*/

 



answered Jul 25, 2017 by avibootz
0 votes
var arr = [
  ["key1", 4],
  ["key2", 2],
  ["key3", 1],
  ["key4", 5],
  ["key5", 3]
];
    
arr = arr.sort(function(a, b) {
  return b[1] - a[1];
})    
    
document.write(JSON.stringify(arr));
    

/*
run:

[["key4",5],["key1",4],["key5",3],["key2",2],["key3",1]]
  
*/

 



answered Jul 25, 2017 by avibootz
0 votes
var arr = [
  ["key1", 4],
  ["key2", 2],
  ["key3", 1],
  ["key4", 5],
  ["key5", 3]
];
    
arr = arr.sort(function(a, b) {
  return a[0] - b[0];
})    
    
document.write(JSON.stringify(arr));
    

/*
run:

[["key1",4],["key2",2],["key3",1],["key4",5],["key5",3]]
  
*/

 



answered Jul 25, 2017 by avibootz
...