• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting an array?

 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we have the following two arrays,we need to treat them as two colunms.
int x[]=new int[]{1, 2, 2, 3, 5, 8, 99, 2};
char y[]=new char[]{'h','c','a','b','t','p','m','n'}
i need to sort them based on values in array x and i will get the following:
int x[]= new int[]{1, 2, 2, 2, 3, 5, 8, 99}
char y[]=new char[]]{'h','c','a','n','b','t','p','m'}
then i need to sort charatcers associated with every group of 2s
as :
int x[]= new int[]{1, 2, 2, 2, 3, 5, 8, 99}
char y[]=new char[]]{'h','a','c','n','b','t','p','m'}
when i say the characters associated with the 2 i mean c,a,n
i need them to be in this order a,c,n...
Thanks for your help.

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to ask why you have stored your values in two arrays? If you create a single array of custom objects (each containing an int, a char, and your own comparison method), you could just use Arrays.sort() on the array.
Is there a compelling reason for representing your data this way?
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
Could you point me in a direction of an example of what your talking about? Ive never seen one implemented, and it sounds very useful for situations like this. Thanks
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here is a solution to the above problem, using these techniques:


[This message has been edited by Frank Carver (edited March 21, 2001).]
 
Marius Holm
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
I didn't quite get your point, you mean you want to sort the characters like 'under' the numbers, as if the numbers are your first sort criteria, then the caracters?
In that case you can do something like:
<PRE>
var numAr=new Array(6,2,7,9,1,2,4,4,3);
var charAr=new Array("c","f","x","d","g","h","w","t","a");
public void sortArrays(int[] numAr,char[] charAr){
System.out.println("numAr: "+numAr);
System.out.println("charAr: "+charAr);

int tmpi;
char tmpc;
for(int m=0;m<numAr.length-1;m++){<br /> System.out.println("for loop, m is now "+m+", numAr["+m+"]="+numAr[m]+", numAr["+(m+1)+"]="+numAr[m+1]);<br /> while(numAr[m]>numAr[m+1]){
tmpi=numAr[m];
numAr[m]=numAr[m+1];
numAr[m+1]=tmpi;
tmpc=charAr[m];
charAr[m]=charAr[m+1];
charAr[m+1]=tmpc;
if(i>0)m--;
System.out.println("Sorting, m is now "+m);
}
}
System.out.println("numAr: "+numAr);
System.out.println("charAr: "+charAr);
for(int m=0;m<numAr.length-1;m++){<br /> System.out.println("charAr["+m+"]="+charAr[m]+", charAr["+(m+1)+"]="+charAr[m+1]+" ?"+(charAr[m]>charAr[m+1]));
while((numAr[m]==numAr[m+1])&&(charAr[m]>charAr[m+1])){
System.out.println("charAr["+m+"]="+charAr[m]+", charAr["+(m+1)+"]="+charAr[m+1]);
tmpc=charAr[m];
charAr[m]=charAr[m+1];
charAr[m+1]=tmpc;
if(i>0)m--;
}
}
System.out.println("numAr: "+numAr);
System.out.println("charAr: "+charAr);
}
</PRE>
Andy,
For instance, like this:
<PRE>
/* Declare this class in your file (at the end)*/
class mySortObject{
int intVal;
char charVal;
public mySortObject(int i,char c){
intVal=i;
charVal=c;
}
}
/* to declare an array of those:*/
mySortObject[] mySortArray;
/* add objects to the Array:
if you know the length when initialising, declare the array like this:*/
mySortObject[] mySortArray=new mySortObject[30];
/*
if not, you must copy the array into a new array with more elements whenever you add, like this:*/
mySortObject[] tmpMSA=new mySortObject[mySortArray.length+1];
for(int m=0;m<mySortArray.length;m++)tmpMSA[m]=mySortArray[m];<br /> /*then add the new Object:*/
tmpMSA[mySortArray.length]=new mySortObject(9,'m');
// and copy to the original Array (if you must)
mySortArray=tmpMSA;
/*Sorting your Array then boils down to:*/
Arrays.sort(mySortArray);
</PRE>
Note: The sort() function sorts after what would be the 'natural ordering' of the elements, what that means in this case you will have to find out...
Regards,
Marius
PS. This UBB code makes it hard to type the program code, indexes for instance...hope I got it right now...
[This message has been edited by Marius Holm (edited March 21, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic