1

I have a 2D string array consisting of values like as

{ "Home","0.1256784"  
 "Contact","-0.56789"  
 "Refer","1.36589"  
 "Next","3.678456" }

I have to sort the array based upon the second element(double value) and obtain a result such as like

{"Contact","-0.56789"  
 "Home","0.1256784"  
 "Refer","1.36589"  
 "Next","3.678456" }  

I have used some bubble sort code to sort it and it works, but and i have to know how can i make the sorting more efficient than my one in faster manner.I tried some code posted previously for the questions related to mine but i can't get the task done.
My Code:

String tt="",tk="";
for(int i=1;i<myarray.length;i++)
 {  
for(int j=1;j<myarray.length-1;j++)
       {
       if(Double.parseDouble(myarray[i][1])<Double.parseDouble(myarray[j][1]))
            {
                    tk=myarray[i][1];  
                    tt=myarray[i][0];  
                    myarray[i][1]=myarray[j][1];  
                    myarray[i][0]=myarray[j][0];  
                    myarray[j][1]=myarray;  
                    myarray[j][0]=myarray;  


            }
          }
        }
1
  • try merge sort it runs in O(n log n) as opposed to O(n^2) like bubble sort: en.wikipedia.org/wiki/Merge_sort Commented Feb 17, 2011 at 9:13

4 Answers 4

1
public class Sort2D {
  public static void main(String args[]) {
    String ss[][] = { 
      {"Home", "0.1256784"},
      {"Contact", "-0.56789"},
      {"Refer", "1.36589"},
      {"Next", "3.678456"}
    };
    Arrays.sort(ss, new Comparator<String[]>() {
      public int compare(String[] s1, String[] s2) {
        double d1 = Double.parseDouble(s1[1]);
        double d2 = Double.parseDouble(s2[1]);
        return Double.compare(d1, d2);
      }
    });
    for (String[] s : ss) {
      System.out.println(s[0] + ": " + s[1]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

why are you overriding equals? other than that solution looks good +1
Thanks for th e reply.I tried the above code and when i run the code some NullPointerException is thrown.It occurs in the line 'Arrays.sort(ss, new Comparator<String[]>()'
@Aly - good catch, I removed the "equals" override; I added it because it appears in the Comparator interface but it is not needed for this simple example because Object provides a default implementation.
@Naveen - this code compiles and runs on its own, you'll have to fit it into your own code structure somehow; I only demonstrated the part you asked about.
I got it worked.The error i made was that i started the array from ss[1][1] instaed of ss[0][1].So it reported NullpointerException on reading ss[0][1].Thanks a lot maerics..
1

If it's a 2d array you can use Array.sort(String[], Comparator<String[]> comparator) and pass a custom comparator, which compares the 2nd element of the sub array.

Comments

1

You can use Arrays.sortsort(Object[] a, Comparator c) and let java take care of it. You may find this link useful

Comments

0

Alternative approach: you could copy the data to a TreeMap (in case all double values are unique) and let the map to the sorting:

Map<Double, String> map = new TreeMap<Double, String>();
for (String[] row:myArray) {
   map.put(Double.parseDouble(row[1]), row[1]);

The entrysets iterator now returns the values in ascending sort order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.