Results 1 to 5 of 5
Thread: Help with sorting arrays
- 03-30-2010, 07:31 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Help with sorting arrays
HEY GUYS I HAVE THIS COMPLIELING BUT I CANT GET IT TOO LOOK RIGHT AND IT GOES INTO THE GETNAME() TWICE BUT I ONLY WANT TO DO IT ONCE HERE IS WHAT I WANT IT TO LOOK LIKE
unsorted:
but i need to sort it to be like this:Java Code:date name atachment ---- ------ ---------- 6 john 7 5 kevin 8 4 jim 4
AS YOU CAN SEE IT IS AN EMAIL IN VOICE BUT I NEED TO SORT THE FROM BIGGEST TO SMALLESTJava Code:date name atachment ---- ------ ---------- 4 jim 4 5 kevin 8 6 john 7
HERE IS MY CODE RUN IT AND YOU WILL SEE WHAT IT LOOK LIKE I RUNS THE NAMES TWICE WHICH I DONT WANT AND I CANT GET IT TOO LOOK RIGHT
as you see i need to print it first then use an other method to sort the numbers and go back in to get the namesJava Code:class WS7QC { public static void main(String args[]) { System.out.print("------------------------------------------------------------------------\n"); int A[][] = new int[5][2]; GetArray(A); System.out.println(" ---------------------------------------------"); SortedArray(A); }//end of main public static void GetArray(int A[][]) { System.out.println(" --------------UNSORTED_EMAIL-----------------"); System.out.println(); System.out.println(" Date: From: Attachments:"); System.out.println(" ---- ----- -----------"); for (int row = 0; row<A.length; row++) { for(int col = 0; col<A[row].length; col++) { A[row][col] =(int)(Math.random( )*10000) % 10+1;//generate randome nuihem in array System.out.print(" "+A[row][col]+ "\t\t\t\t "); if(col == 0) { GetName(A,row); } } //end of loop System.out.println(); }//end of first loop }//end of procedure public static void GetName(int A[][],int row) { for(int i=0;i<A[row].length;i++){ switch(A[row][0]) { case 1:System.out.println(" Kevin \t\t ");break; case 2:System.out.println(" Ian \t\t ");break; case 3:System.out.println(" Andy \t\t ");break; case 4:System.out.println(" James \t\t ");break; case 5:System.out.println(" Dave \t\t ");break; case 6:System.out.println(" Conor \t\t ");break; case 7:System.out.println(" Shane \t\t ");break; case 8:System.out.println(" Ciarn \t\t ");break; case 9:System.out.println(" Christian \t ");break; case 10:System.out.println(" Barry \t\t "); }//end of switch } }//end of procedure 'GetNmane' public static void SortedArray(int A[][]) { System.out.println(" --------------UNSORTED_EMAIL-----------------"); System.out.println(); System.out.println(" Date: From: Attachments:"); System.out.println(" ---- ----- -----------"); /* int hold,hold1,hold2,hold3,hold4; for(int r=0; r<A.length-1; r++) { for(int c=0; c<A.length-r-1; c++) { if (A[c][0]>A[c+1][0]) { hold=A[c][0]; hold1=A[c][1]; hold2=A[c][2]; hold3=A[c][3]; hold4=A[c][4]; A[c][0]=A[c+1][0]; A[c][1]=A[c+1][1]; A[c][2]=A[c+1][2]; A[c][3]=A[c+1][3]; A[c][4]=A[c+1][4]; A[c+1][0]=hold; A[c+1][1]=hold1; A[c+1][2]=hold2; A[c+1][3]=hold3; A[c+1][4]=hold4; }// end of if */ }//end of sort }//end of class
CAN SOMEONE PLEASE HELPLast edited by Joycey; 03-30-2010 at 07:38 PM.
- 03-30-2010, 07:36 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Remember the sorted array method is commonts not code
- 03-30-2010, 08:00 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
There is no reason to shout. Those date, name and attachment are details of something larger: an email specification so I had expected something like this:
Next I expected a simple one dimensional array like this:Java Code:public class Email { private int date; private String name; private int attachment; ... // constructor(s) and getters etc. go here }
You have to sort this array. The Arrays class has a sort( ... ) method that can do the job. Also read the API documentation for the Comparator interface.Java Code:Email[] emails= ...
kind regards,
Jos
- 03-30-2010, 08:34 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
hey
we havent learned any of that yet
- 03-30-2010, 08:35 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Sorting Two Arrays
By Faye Rett in forum New To JavaReplies: 4Last Post: 03-07-2010, 01:00 AM -
Problem: Arrays and Sorting
By Rhez in forum New To JavaReplies: 7Last Post: 02-03-2010, 02:18 PM -
Need help with sorting 2d arrays (insertion and selection)
By mjunit in forum New To JavaReplies: 0Last Post: 01-17-2010, 06:09 AM -
Sorting Arrays by enum
By sahity1a@yahoo.com in forum New To JavaReplies: 3Last Post: 11-26-2009, 09:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks