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 OverflowIt'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;
}
}
The best approach would be to use the following, as there may be repetitive values in the first column.
var arr = [[12, 'AAA'], [12, 'BBB'], [12, 'CCC'],[28, 'DDD'], [18, 'CCC'],[12, 'DDD'],[18, 'CCC'],[28, 'DDD'],[28, 'DDD'],[58, 'BBB'],[68, 'BBB'],[78, 'BBB']];
arr.sort(function(a,b) {
return a[0]-b[0]
});
[JavaScript]How can I sort the data in a two dimensional array from array[x][y] to array[y][x] (better explanation in text)
JavaScript sort 2D array
Sort 2D array of pairs in JavaScript - Stack Overflow
Simple way to sort 2d array in Javascript? - Stack Overflow
Let's say I'm given:
array[0] = [A, B, C] array[1] = [A, B, C] array[2] = [A, B, C]
And what I want is:
newArray[1] = [A, A, A] newArray[2] = [B, B, B] newArray[3] = [C, C, C]
What would be the best way to do this? Whenever I try to do a simple
let array = [];
array[0] = [A, B, C];
array[1] = [A, B, C];
array[2] = [A, B, C];
let newArray = [];
for (x = 0; x < 2; x++) {
for (y = 0; y < 2; y++) {
newArray[x][y] = array[y][x];
}
}I get told "Cannot set property '0' of undefined"
You could sort by the delta of the first and second index values.
const array = [[0, 23], [19, 30], [6, 47], [5, 59], [1, 56], [1, 20], [19, 10]];
array.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
console.log(array);
let arr = [[0,23],[19,30],[6,47],[5,59],[1,56],[1,20],[19,10]];
let result = arr
// .map(a => `${a[0]}.${a[1]}`)
.sort((a,b)=> {
if (a[0] === b[0]) {
return a[1] - b[1];
} else {
return a[0] - b[0];
}
})
// .map(a => [parseInt(a.split('.')[0]),parseInt(a.split('.')[1])]);
console.log(result);
.as-console-row-code {
white-space: initial !important;
}
I have commented the map statemens not to convert them into strings. that makes it sorted lexicographically. We can use custom sort function here as shown above
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
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.
You can provide sort with a comparison function.
showIt.sort(function(a, b) {
return a[1] - b[1];
});
a and b are items from your array. sort expects a return value that is greater than zero, equal to zero, or less than zero. The first indicates a comes before b, zero means they are equal, and the last option means b first.
This site advises against using the arguments without assigning to temporary variables. Try this instead:
showIt.sort(function(a, b) {
var x = a[1];
var y = b[1];
return x - y;
});
