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.
Page Index Toggle Pages: 1
Sort 2d array (Read 1859 times)
Sort 2d array
Nov 27th, 2008, 6:23pm
 
Hi board,
I have this 2d array of rgb values.
I like to sort them from the highest rgb[i][2] value to the lowest. Does anyone can help me, because I don´t know the correct sort() syntax for 2d arrays...

float[][] rgb = new float[3][3];
rgb[0][0] = 100.0;
rgb[0][1] = 100.0;
rgb[0][2] = 255.0;

rgb[1][0] = 12.0;
rgb[1][1] = 45.0;
rgb[1][2] = 200.0;

rgb[2][0] = 111.0;
rgb[2][1] = 12.0;
rgb[2][2] = 251.0;

for(int i = 0; i < rgb.length; i++) {
 fill(rgb[i][0], rgb[i][1], rgb[i][2]);
 rect(20+i*20,20,20,20);
 println( "r= "+rgb[i][0] + ", g= " + rgb[i][1] + ", b= " + rgb[i][2]);
}
Re: Sort 2d array
Reply #1 - Nov 27th, 2008, 9:14pm
 
I think there are two problems here: one is the sorting of 2D matrix, and another is the overall goal to the code.

You may have to look for or implement your own 2D matrix sorting algorithm, which would be kind of painful in this environment (I may be wrong - anyone?).

For sorting RGB values: You may want to create an RGB type and handle that as an object, then sort that with a comparator that uses its blue value.  So it may look like:


class Rgb{
 Float r, g, b;
 
 Rgb(Float r, Float g, Float b){
   this.r = r;
   this.g = g;
   this.b = g; //this.g = b;
 }
}

class RgbBlueComparator implements Comparator{
public int compare(Object o1, Object o2) {
               Float i = ((Rgb)o1).b -  ((Rgb)o2).b;
return i.intValue();
}
}


Again, use of generics will make the second chunk a lot more readable, so you may want to implement this in a .java file.
Re: Sort 2d array
Reply #2 - Nov 28th, 2008, 12:22pm
 
Thank you sw01!
I see what the problem is and this will help me. So I will try out to solve the problem with your way.
Re: Sort 2d array
Reply #3 - Nov 28th, 2008, 1:25pm
 
Aah, I wondered why you used a Comparator instead of making RGB implementing Comparable.
Found a good explanation at java.util.Comparable vs java.util.Comparator... Smiley
Of course, your method is the best in this case, because one might want to implement RgbRedComparator and RgbGreenComparator as well.
Re: Sort 2d array
Reply #4 - Nov 28th, 2008, 4:26pm
 
Toxi posts a sorting algorythm for colors long time ago:

http://www.toxi.co.uk/blog/2006/04/colour-code-snippets.htm
Re: Sort 2d array
Reply #5 - Nov 28th, 2008, 7:52pm
 
Hi,

Sorry, I found an embarrassing bug. The class Rgb should be:

Rgb{
 Float r, g, b;
 
 Rgb(Float r, Float g, Float b){
   this.r = r;
   this.g = g;
   this.b = b;
 }
}

Instead of // this.g = b;

Teaches me a lesson: I shouldn't be using single letter variables.  :)

Just in case cut and paste is used.



< PhiLho: Thank you for the followup.

I should have explained that this was intended to be used with Collections.sort(List l, Comparator c), where List can be ArrayList.  That's what happens when one talks to machines for too long...
Re: Sort 2d array
Reply #6 - Jan 14th, 2010, 6:33am
 
Code for sorting 2d array.  Just change the if statement in the for loop to whatever item you want to sort on.

boolean sorted=false;
 do{
   sorted=true;
   for(index=0;index<tempPlantCounter-1;index++){
     if(tempPlant[7][index]>tempPlant[7][index+1]){  //if plant before costs more, swap places
      float bigSummerCapacity = tempPlant[0][index];
      float bigHeatRate = tempPlant[1][index];
      float bigVom = tempPlant[2][index];
      float bigFom = tempPlant[3][index];
      float bigCapacityFactor = tempPlant[4][index];
      float bigPolicyCost = tempPlant[5][index];
      float bigEnergyCost = tempPlant[6][index];
      float bigTotalCost = tempPlant[7][index];
     
      int i;
      for(i=0;i<8;i++){
       tempPlant[i][index]=tempPlant[i][index+1];
      }
      //tempPlant[5][index]=tempPlant[5][index+1];
      //tempPlant[6][index]=tempPlant[6][index+1];
      //tempPlant[7][index]=tempPlant[7][index+1];
      tempPlant[0][index+1]=bigSummerCapacity;
      tempPlant[1][index+1]=bigHeatRate;
      tempPlant[2][index+1]=bigVom;
      tempPlant[3][index+1]=bigFom;
      tempPlant[4][index+1]=bigCapacityFactor;
      tempPlant[5][index+1]=bigPolicyCost;
      tempPlant[6][index+1]=bigEnergyCost;
      tempPlant[7][index+1]=bigTotalCost;
      sorted=false;
     }
   }
 }
 while(sorted==false);
Re: Sort 2d array
Reply #7 - Jan 14th, 2010, 6:39am
 
You re-invent a sort algorithm, not the fastest either...
And when you start to have an array of values mapping to various information, as the temporary variable names implies, you should start looking into classes. Smiley
Re: Sort 2d array
Reply #8 - Feb 17th, 2010, 3:23am
 
brEnergy, could you explain that a little more?

It does this?

from
Code:

String[][] databaseToArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"John", "Nick", "likes", "2", "yes" },
{"Drew", "MTV", "dislikes", "4", "no" },
{"Fred", "CNN", "okay", "3", "no" },
{"Beth", "Fox", "valid", "1", "yes" }
};


to
Code:

String[][] reorganizedArray = {
//{"Name", "Channel", "Description", "Amount", "isReady"},
{"Beth", "Fox", "valid", "1", "yes" },
{"John", "Nick", "likes", "2", "yes" },
{"Fred", "CNN", "okay", "3", "no" },
{"Drew", "MTV", "dislikes", "4", "no" }
};
Page Index Toggle Pages: 1