We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › sort the 2nd column of a 2d
Page Index Toggle Pages: 1
sort the 2nd column of a 2d (Read 613 times)
sort the 2nd column of a 2d
May 8th, 2007, 6:43am
 
my objective is to sort a 2d array – that has 3 rows and 2 columns – on the 2nd column. step 4 is where i could use some help.

// 1. init environment
int[][] distances;
int[][] b;
int r = 3;
int c = 2;

size(200,300);
background(0);
distances = new int[r][c];

// 2. populate both columns of the 2d array
for(int i=0; i<r-1; i++) {
 // indices for reference later on
 distances[i][0] = i;    
 // random values that will need to be sorted next...
 distances[i][1] = 50+i;  
}

// 3. print the results to confirm it's populated correctly
for(int i=0; i<r-1; i++) {
 println(distances[i][0]);  
 println(distances[i][1]);
 println();
}

// 4. sort the array
// b = sort(distances);
// b = sort(distances[]);
// b = sort(distances[:]);
// b = sort(distances[][]);
// b = sort(distances{});
// b = sort(distances[]{});
// b = sort(distances[]{[]});
// hmmmmm?


// 5. print results again to confirm the array is sorted properly
for(int i=0; i<r-1; i++) {
 println(distances[i][0]);  
 println(distances[i][1]);
 println();
}



thanks in advance,

woohoo!
drew
Re: sort the 2nd column of a 2d
Reply #1 - May 15th, 2007, 8:53pm
 
drwoohoo,

first, "Arrays are similar to objects, so they must be created with the keyword new" (reference section of this page).

int[][] distances = new int[//amount of columns][//amount of rows];

second, there are different sorting algorithms; differing in time complexity (efficiency): selection sort, bubble sort, insertion sort, merge sort, and quick sort.

Good luck, /BEnm
Re: sort the 2nd column of a 2d
Reply #2 - Apr 23rd, 2008, 3:20pm
 
Hi,
I was wondering if someone could clarify a way to do this? I have a two-dimensional array and want to sort by the second column.

eg:

Count1, Value1
Count2, Value2
Count3, Value3

I would like to sort the entire array by the Value so that the respective Count remains connected to the Value. Efficiency is not the most important thing as it will only be run once.

Any hints or help would be greatly appreciated!

Thank you!
Page Index Toggle Pages: 1