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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: sorting only 1 column of 2D array

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sorting only 1 column of 2D array

    Can you post the definition of an array to sort as java code for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sorting only 1 column of 2D array

    Quote Originally Posted by Norm View Post
    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. #6
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. sum of each row & column in 2-dimensional array.
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 08:09 AM
  2. Replies: 2
    Last Post: December 26th, 2012, 07:01 PM
  3. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  4. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM