4 Replies - 3427 Views - Last Post: 06 June 2009 - 06:46 AM Rate Topic: -----

#1 Randhawa_boyz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 09

Descending Order sorting of an 2D array

Posted 02 June 2009 - 10:12 PM

import java.util.*;
public class Employee
{
public static void main(String args[])
{
int iArray[] = new int[8];
int kArray[] = new int[8];


int[][] DaysWorked =new int[8][8];
DaysWorked[1][1] =2;
DaysWorked[1][2] =4;
DaysWorked[1][3] =3;
DaysWorked[1][4] =4;
DaysWorked[1][5] =5;
DaysWorked[1][6] =8;
DaysWorked[1][7] =8;
DaysWorked[2][1] =7;
DaysWorked[2][2] =3;
DaysWorked[2][3] =4;
DaysWorked[2][4] =3;
DaysWorked[2][5] =3;
DaysWorked[2][6] =4;
DaysWorked[2][7] =4;
DaysWorked[3][1] =3;
DaysWorked[3][2] =3;
DaysWorked[3][3] =4;
DaysWorked[3][4] =3;
DaysWorked[3][5] =3;
DaysWorked[3][6] =2;
DaysWorked[3][7] =2;
DaysWorked[4][1] =9;
DaysWorked[4][2] =3;
DaysWorked[4][3] =4;
DaysWorked[4][4] =7;
DaysWorked[4][5] =3;
DaysWorked[4][6] =4;
DaysWorked[4][7] =1;
DaysWorked[5][1] =3;
DaysWorked[5][2] =5;
DaysWorked[5][3] =4;
DaysWorked[5][4] =3;
DaysWorked[5][5] =6;
DaysWorked[5][6] =3;
DaysWorked[5][7] =8;
DaysWorked[5][1] =3;
DaysWorked[5][2] =4;
DaysWorked[5][3] =4;
DaysWorked[5][4] =6;
DaysWorked[5][5] =3;
DaysWorked[5][6] =4;
DaysWorked[5][7] =4;
DaysWorked[6][1] =3;
DaysWorked[6][2] =7;
DaysWorked[6][3] =4;
DaysWorked[6][4] =8;
DaysWorked[6][5] =3;
DaysWorked[6][6] =8;
DaysWorked[6][7] =4;
DaysWorked[7][1] =6;
DaysWorked[7][2] =3;
DaysWorked[7][3] =5;
DaysWorked[7][4] =9;
DaysWorked[7][5] =2;
DaysWorked[7][6] =7;
DaysWorked[7][7] =9;

Arrays.sort(DaysWorked);
for (int i=0;i<DaysWorked.length ;i++ )
{
for (int j =0;j<DaysWorked.length ;j++ )
{

DaysWorked daysWorked = DaysWorked[i][j];
}
System.out.println(+DaysWorked[i][j]);
}









}
}

this doesn't work could u please rt it ,i want to sort it in descending order

This post has been edited by Randhawa_boyz: 02 June 2009 - 10:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Descending Order sorting of an 2D array

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Descending Order sorting of an 2D array

Posted 02 June 2009 - 10:30 PM

[rules][/rules]

I know you "posted your code"...but we need some sort of willingness to learn the material from you. We won't do your homework for you.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Descending Order sorting of an 2D array

Posted 05 June 2009 - 07:19 PM

Don't really understand what you want to do

for (int j =0;j<DaysWorked.length;j++ )
{
	  // this creates a NEW DaysWorked object named daysWorked
	  DaysWorked daysWorked = DaysWorked[i][j];
}	 <---- that stops to exist here.... so kind of useless 



And please :code:

This post has been edited by pbl: 05 June 2009 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Descending Order sorting of an 2D array

Posted 05 June 2009 - 08:10 PM

Check here:

http://ranganacoding...array-using.htm
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Descending Order sorting of an 2D array

Posted 06 June 2009 - 06:46 AM

You can use this sort of approach, here sorted by the column at index 1:

        Integer[][] DaysWorked = new Integer[8][8];
        DaysWorked[0][1] = Integer.MIN_VALUE; // This is here as all rows must be present to avoid NullPointerException
        DaysWorked[1][1] = 2;
        DaysWorked[1][2] = 4;
        DaysWorked[1][3] = 3;
        DaysWorked[1][4] = 4;
        DaysWorked[1][5] = 5;
        DaysWorked[1][6] = 8;
        DaysWorked[1][7] = 8;
        DaysWorked[2][1] = 7;
        DaysWorked[2][2] = 3;
        DaysWorked[2][3] = 4;
        DaysWorked[2][4] = 3;
        DaysWorked[2][5] = 3;
        DaysWorked[2][6] = 4;
        DaysWorked[2][7] = 4;
        DaysWorked[3][1] = 3;
        DaysWorked[3][2] = 3;
        DaysWorked[3][3] = 4;
        DaysWorked[3][4] = 3;
        DaysWorked[3][5] = 3;
        DaysWorked[3][6] = 2;
        DaysWorked[3][7] = 2;
        DaysWorked[4][1] = 9;
        DaysWorked[4][2] = 3;
        DaysWorked[4][3] = 4;
        DaysWorked[4][4] = 7;
        DaysWorked[4][5] = 3;
        DaysWorked[4][6] = 4;
        DaysWorked[4][7] = 1;
        DaysWorked[5][1] = 3;
        DaysWorked[5][2] = 5;
        DaysWorked[5][3] = 4;
        DaysWorked[5][4] = 3;
        DaysWorked[5][5] = 6;
        DaysWorked[5][6] = 3;
        DaysWorked[5][7] = 8;
        DaysWorked[5][1] = 3;
        DaysWorked[5][2] = 4;
        DaysWorked[5][3] = 4;
        DaysWorked[5][4] = 6;
        DaysWorked[5][5] = 3;
        DaysWorked[5][6] = 4;
        DaysWorked[5][7] = 4;
        DaysWorked[6][1] = 3;
        DaysWorked[6][2] = 7;
        DaysWorked[6][3] = 4;
        DaysWorked[6][4] = 8;
        DaysWorked[6][5] = 3;
        DaysWorked[6][6] = 8;
        DaysWorked[6][7] = 4;
        DaysWorked[7][1] = 6;
        DaysWorked[7][2] = 3;
        DaysWorked[7][3] = 5;
        DaysWorked[7][4] = 9;
        DaysWorked[7][5] = 2;
        DaysWorked[7][6] = 7;
        DaysWorked[7][7] = 9;

        Arrays.sort(DaysWorked,
            new Comparator<Integer[]>() {
                public int compare(Integer[] a1, Integer[] a2) {
                    //return a1[1].compareTo(a2[1]); // Ascending
                    return a2[1].compareTo(a1[1]); // Descending
                }
            });
	for(Integer[] days : DaysWorked) {
	    System.out.println(Arrays.toString(days));
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1