Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: sorting only 1 column of 2D array

1. ## sorting only 1 column of 2D array

hi.. i am trying to sort my 2D array such that only the 2nd column get sorted.. i tried to use Arrays.sort(arr[a]) but it seem to sort the whole array

example of what i want:
before sort
a[0][0]= 9 a[1][0] = 50
a[0][1]= 5 a[1][1] = 5
a[0][2]= 3 a[1][2] = 78
a[0][3]= 6 a[1][3] = 23
a[0][4]= 2 a[1][4] = 67

aft sort
a[0][0]= 2 a[1][0] = 5
a[0][1]= 3 a[1][1] = 23
a[0][2]= 5 a[1][2] = 50
a[0][3]= 6 a[1][3] = 67
a[0][4]= 9 a[1][4] = 78

the code i m using now
``` for (int i=0;i<dist.length;i++)
{
Arrays.sort(dist[i]);
}```

output of current code:
before sort
a[0][0]= 9 a[1][0] = 50
a[0][1]= 5 a[1][1] = 5
a[0][2]= 3 a[1][2] = 78
a[0][3]= 6 a[1][3] = 23
a[0][4]= 2 a[1][4] = 67

aft sort
a[0][0]= 2 a[1][0] = 9
a[0][1]= 3 a[1][1] = 23
a[0][2]= 5 a[1][2] = 50
a[0][3]= 5 a[1][3] = 67
a[0][4]= 6 a[1][4] = 78

2. ## Re: sorting only 1 column of 2D array

Can you post the definition of an array to sort as java code for testing?

3. ## Re: sorting only 1 column of 2D array

` double[][] a1 = new double[][] {{2.5,3.6,0.7,0.01},{0.85,1.32,0.20,0.992}};`

based on this the output after sorting should be
a1[0][0] = 0.01 a1[1][0] = 0.20
a1[0][1] = 0.7 a1[1][0] = 0.85
a1[0][2] = 2.5 a1[1][0] = 0.992
a1[0][3] = 3.6 a1[1][0] = 1.32

4. ## Re: sorting only 1 column of 2D array

When I execute this code:
```      double[][] dist = new double[][] {{2.5, 3.6, 0.7, 0.01},{0.85, 1.32, 0.20, 0.992}};
System.out.println("B="+Arrays.deepToString(dist));
for (int i=0;i<dist.length;i++)
{
Arrays.sort(dist[i]);
}
System.out.println("A="+Arrays.deepToString(dist));```
I get this printed:
B=[[2.5, 3.6, 0.7, 0.01], [0.85, 1.32, 0.2, 0.992]]
A=[[0.01, 0.7, 2.5, 3.6], [0.2, 0.85, 0.992, 1.32]]
It looks sorted to me.

5. ## Re: sorting only 1 column of 2D array

Originally Posted by Norm
When I execute this code:
```      double[][] dist = new double[][] {{2.5, 3.6, 0.7, 0.01},{0.85, 1.32, 0.20, 0.992}};
System.out.println("B="+Arrays.deepToString(dist));
for (int i=0;i<dist.length;i++)
{
Arrays.sort(dist[i]);
}
System.out.println("A="+Arrays.deepToString(dist));```
I get this printed:

It looks sorted to me.
Weird... As stated in my first post. I tried your method and did not get your result. Will check where I did wrong

6. ## Re: sorting only 1 column of 2D array

thanks for all the help... think the error i 1st encounter is my stupid self trying to display a shorter version of my array.. did not realise that the value is hidden in the array index i did not wan to be displayed out...