0

I know that this may be an easy question,but I could not come up with an appropriate algorithm. I have an 2d array of strings and I would like to sort it by the number of elements: Assuming the array is as follows:

public class arraysort 

  {

    public static void main(String[] args)

      {
        String[][] terms = {{"java", "php", "ruby", "csharp", "dotnet", "perl"},
                            {"google", "apple", "oracle", "microsoft", "sun"},
                            {"http", "web", "dns", "net", "protocol", "packet","ip"},
                            {"london","madrid","berlin","ankara","astana"}};



      }
  }

How can I get sorted array by the number of elements in this way(5,5,6,7):

[google, apple, oracle, microsoft, sun]
[london, madrid, berlin, ankara, astana]
[java, php, ruby, csharp, dotnet, perl]
[http, web, dns, net, protocol, packet, ip]

Also, it is interesting for me, what happens when the number of elements is equal in each group such as "google" and "london" groups have equal number of elements. Thank you for your help!

2 Answers 2

3

Use java.util.Arrays.sort(...) (the Arrays class API link). One of the method overloads takes an array parameter with a Comparator parameter, in in your Comparator's compare(...) method, compare the lengths of the sub arrays.

e.g.,

Arrays.sort(terms, (a1, a2) -> Integer.compare(a1.length, a2.length));

e.g.,

import java.util.Arrays;

public class Sort2DArrays {
    public static void main(String[] args) {
        String[][] terms = { { "java", "php", "ruby", "csharp", "dotnet", "perl" },
                { "google", "apple", "oracle", "microsoft", "sun" },
                { "http", "web", "dns", "net", "protocol", "packet", "ip" },
                { "london", "madrid", "berlin", "ankara", "astana" } };

        Arrays.sort(terms, (a1, a2) -> Integer.compare(a1.length, a2.length));

        for (String[] term : terms) {
            System.out.println(Arrays.toString(term) + ", length: " + term.length);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for answer @Hovercraft Full Of Eels. But how can it be done without using Arrays.sort() method. For example, how can we sort it by bubble sort ?
@JohnDoe: by writing your own bubble sort code that does this. It's easy to search this site on sorting an array with bubble sort, for example: Search of site on bubble sort
Hmm ok, I will search and adapt it to my code. Thanks for all help!
0

Bubble Sort

import java.util.Arrays;

public class BubbleSort {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
     String[][] terms = { { "java", "php", "ruby", "csharp", "dotnet", "perl" },
                { "google", "apple", "oracle", "microsoft", "sun" },
                { "http", "web", "dns", "net", "protocol", "packet", "ip" },
                { "london", "madrid", "berlin", "ankara", "astana" } };
     // The cycle time will put the biggest that number in the I That is the highest one .
     //That is why the algorithm called Bubble Sort, because every time like bubbles rise.
     for(int i =terms.length-1;i>=0;--i)
     {
         //The cycle time will traverse 0--(i-1),And when comparing the size of the adjacent two.
         //they will be big like bubbles rise up, and is actually exchange two object
         for(int j=0;j<=i-1;j++)
         {
             if (terms[j].length>terms[j+1].length) {
                 String[] term= terms[j];
                 terms[j] = terms[j+1];
                 terms[j+1]= term;
            }

         }
     }
     for (String[] term : terms) {
            System.out.println(Arrays.toString(term) + ", length: " + term.length);
        }

}

}

4 Comments

Thank you very much @abner.zhang !
Spoon-fed code only answers are as bad as code-only questions. At least explain what you're doing.
@Hovercraft Full Of Eels:Thank for you proposal.I will go to explain what am I doing,but my English is very bad.
Please let me know when you do so I can up-vote it if it looks good!

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.