It's this simple:

var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']];

a.sort(sortFunction);

function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    }
    else {
        return (a[0] < b[0]) ? -1 : 1;
    }
}

I invite you to read the documentation.

If you want to sort by the second column, you can do this:

a.sort(compareSecondColumn);

function compareSecondColumn(a, b) {
    if (a[1] === b[1]) {
        return 0;
    }
    else {
        return (a[1] < b[1]) ? -1 : 1;
    }
}
Answer from jahroy on Stack Overflow
🌐
Lage
lage.us › Javascript-Sort-2d-Array-by-Column.html
Sort a Javascript 2d Array by Any Column Value - Lage.us
// given the array array = [ ["Tiger ... "Integration", "New York", "61", "2012/12/02"] ]; // set c to the column number you want to sort // c=0; will sort the first column // c=1; will sort the second column ......
Discussions

[JavaScript]How can I sort the data in a two dimensional array from array[x][y] to array[y][x] (better explanation in text)
newArray[x][y] = array[y][x]; When you write to array[0][0], array[0] isn't even a thing yet. So you have an array, at position 0 there is nothing/null, and you want to set property 0 of null to something, but that isn't going to work. You either write something to array[0] first, an empty array, or you instantiate it as a multidimensional array in the first place. https://stackoverflow.com/questions/16512182/how-to-create-empty-2d-array-in-javascript After that there will still be a small bug in your code, but you can probably figure that one out yourself! More on reddit.com
🌐 r/learnprogramming
1
1
October 15, 2020
JavaScript sort 2D array
So I have an array in JS which looks something like this: let arr = [["p1",5],["p2",7],["p3",2]] How would I sort it in descending order by the second value? So that it would look like this: [["p3",2],["p1",5],["p2",7]] Thanks · Learn more efficiently, for free: More on sololearn.com
🌐 sololearn.com
3
3
Sort 2D array of pairs in JavaScript - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I need to sort 2D array of pairs in JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
Simple way to sort 2d array in Javascript? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › sorting-only-columns-of-a-2-d-array-in-javascript
Sorting only columns of a 2-D array in JavaScript
const arr = [ [6, 2, 9], [8, 1, 4], [5, 3, 7] ]; const sortColumns = (arr = []) => { const transpose = (matrix = []) => { const res = []; for (let row = 0; row < matrix.length; row++) { for (let col = 0; col < matrix[row].length; col++) { if(!res[col]){ res[col] = []; } res[col][row] = matrix[row][col]; } } return res; }; arr = transpose(arr); for (let row = 0; row < arr.length; row++) { arr[row].sort((a, b) => b - a); } return transpose(arr); }; console.log(sortColumns(arr));
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › grouping-and-sorting-2-d-array-in-javascript
Grouping and sorting 2-D array in JavaScript
October 12, 2020 - const arr = [ [1, 3, 2], [5, 2, 1, 4], [2, 1] ]; const groupAndSortModern = arr => { const flattened = arr.flat(); const grouped = flattened.reduce((acc, num) => { if (!acc[num]) acc[num] = []; acc[num].push(num); return acc; }, {}); return Object.keys(grouped) .sort((a, b) => a - b) .map(key => grouped[key]); }; console.log(groupAndSortModern(arr)); ... Both approaches effectively group and sort 2D arrays.
🌐
DEV Community
dev.to › prakash1245 › javascript-sorting-multidimensional-array-5g23
JavaScript Sorting Multidimensional Array - DEV Community
June 1, 2025 - #webdev #programming #javascript #productivity · Sorting is a technique to arrange item to Increasing or Decreasing order · Sorting of item done by comparison of another item or element · Single Dimensional Array have sorted by javascript sort method · Single dimension array sorting output ·
Top answer
1 of 2
1

This is the answer from @Brian. Thank you very much Brian

#include <stdio.h>

void bubbleSort2D(int numRows, int numCols, int arr[][numCols], int col, int incDec) {
  // Using bubble sort to sort a column in the matrix
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numRows - 1; j++) {
      int a = arr[j][col];
      int b = arr[j + 1][col];
      if ((incDec && a > b) || (!incDec && a < b)) {
        arr[j][col] = b;
        arr[j + 1][col] = a;
      }
    }
  }
}

int main() {

  int arr[4][3] = {
    {1,2,3},
    {4,8,6},
    {2,1,5},
    {3,7,4}
  };

  for (int i = 0; i < 3; i++) {
    // sort "odd" columns in descending order, "even" columns in ascending order
    bubbleSort2D(4, 3, arr, i, i % 2 == 0);
  }

  printf("Your sorted array: \n");
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
      printf("%d,", arr[i][j]);
    }
    printf("\n");
  }

  return 0;
}

Here's repl link:

Srort columns in 2D array

2 of 2
0

Not a complete answer, since it looks like you want to solve this problem on your own.

The trick here is to sort by columns, when C stores arrays in row-major order. You can do this by defining a stride (for an array witn M columns and N rows, this would be sizeof a[0], that is, the size of a single row) and doing all indexing in multiples of that stride. Or you can transpose the array, sort by rows, and transpose the result back.

I would recommend sorting in place for performance, but writing the code that does round-trip transposition will probably be simpler. Copying entire arrays twice is expensive, but not incredibly so: it’s proportional to the size of the array.

If you write functions to sort in ascending order and in descending order, and you have an int[M][N], you can call sort_ascending( int[i], N ) or sort_descending( int[i], N ) as appropriate. You might have a look at qsort() in the standard library. If you want to try to sort in place, you’d need to pass the number of rows, the number of columns, and the column to sort. The number of columns would be the stride between the elements you care about.

A few different ways to control whether you sort rows up or down: always sort decreasing then increasing and increment by 2, keep a flag that remembers whether you sorted the last row ascending or descending, or check 1%2, which optimizes to an extremely cheap bitwise or instruction.

🌐
Stack Overflow
stackoverflow.com › questions › 51168768 › how-can-i-sort-2d-array-with-javascript
How can I sort 2D array with JavaScript? - Stack Overflow
Array.prototype.sort() expects a number. So for numbers: the same strategy applies. For strings: numbers must be passed manually based on > / < / == comparison. There are examples of each in the documentation I linked @Lynn 2018-07-04T08:40:50.447Z+00:00 ... Save this answer. ... Show activity on this post. since arr[1] is a date you need to compare them using new Date() like :
🌐
SitePoint
sitepoint.com › javascript
Problem sorting a two-dimensional array - JavaScript - SitePoint Forums | Web Development & Design Community
October 13, 2010 - On closer inspection, I see why your mySorting function is not infinitely recursive. The isArray test simply strips out dimensions until there are none left. ... Another way to deal with this is to restructure the array for sorting purposes, and then once sorted to restructure again back to the original array.
🌐
Rip Tutorial
riptutorial.com › sorting multidimensional array
JavaScript Tutorial => Sorting multidimensional array
Be aware that the sort method operates on the array in place. It changes the array. Most other array methods return a new array, leaving the original one intact.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-sort-the-2d-array-across-rows
JavaScript Program to Sort the 2D Array Across Rows - GeeksforGeeks
August 5, 2025 - In this approach, we use the insertion sort algorithm to iterate through each row of the array. Here we apply Insertion Sort to each row, which iteratively places each element in its correct position within the sorted subarray to its left.
🌐
Coderwall
coderwall.com › p › 5fu9xw › how-to-sort-multidimensional-array-using-javascript
How to sort multidimensional array using javascript (Example)
February 25, 2016 - if( typeof helper == 'undefined' ) { var helper = { } ; } helper.arr = { /** * Function to sort multidimensional array * * param {array} [arr] Source array * param {array} [columns] List of columns to sort * param {array} [order_by] List of directions (ASC, DESC) * returns {array} */ multisort: function(arr, columns, order_by) { if(typeof columns == 'undefined') { columns = [] for(x=0;x<arr[0].length;x++) { columns.push(x); } } if(typeof order_by == 'undefined') { order_by = [] for(x=0;x<arr[0].length;x++) { order_by.push('ASC'); } } function multisort_recursive(a,b,columns,order_by,index) { var direction = order_by[index] == 'DESC' ?
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Question about sorting 2D array - Curriculum Help - The freeCodeCamp Forum
July 30, 2022 - Lets say I have 2D array(example will be from one of the code prep challenges). let testArr = [ [ 88, 'Bowling Ball' ], [ 2, 'Dirty Sock' ], [ 3, 'Hair Pin' ], [ 5, 'Microphone' ], [ 3, 'Half-Eaten Apple' ], [ 7, 'Toothpaste' ] ] I want to sort it alphabetically, by the elements with indexes [1](or I can say strings).