Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting two dimensional String array as per LastName then on FirstName without using any API

Tags:

java

arrays

Hi all I am very new for the Java. I would like to sort below array of strings as per LastName then on FirstName without use of any API i.e. I am not supposed to use Arrays.sort() , compareTo(), equals() etc..

Input array String

String [][]name={{"Jen","Eric"},
              {"Brain","Adams"},
              {"Jon","Methew"},
              {"Antino","Ronald"},
              {"Cris","Ronald"}
             };

my out put should be like.

             Brain,Adams
             Jen,Eric
             Jon,Methew
             Antino,Ronald
             Cris,Ronald

Please Help.

public class StringArraySort {

public static void main(String[] args) {



    //System.out.println(str.length);
    String [][]name={{"Jen","Eric"},
            {"Brain","Adams"},
            {"Jon","Methew"},
            {"Antino","Ronald"},
              {"Cris","Ronald"}
           };

    String []str1= new String [name.length];
    String []str2= new String [name.length];

    for(int i=1;i<name.length;i++)
    {
        int j=i;
        str1[i]=name[i][j];
        str2[i]=name[i-1][j];
        //System.out.println(str1[i]+" "+str2[i]);


    }

    /*for(String tmp:name)
    {
        char a[] = new char[tmp.length()] ;
        //System.out.println(tmp);
        for(int i=0;i<tmp.length();i++)
        {
            a[i]=tmp.charAt(i);
            System.out.println(a[i]);
        }
    }*/
}

}

like image 392
PKS Avatar asked Mar 04 '26 00:03

PKS


2 Answers

I will not give you any code, as this is clearly an assignment, but here's some general guidance:

  1. Don't try to put everything into main. You may not be allowed to use any exiting API, but you can define your own! Write your own compare and sort methods.
  2. Start with a method compare(String, String) -> int, or isSmaller(String, String) -> boolean. Use String.toCharArray to get the individual characters and compare them, in pairs from both strings. Make sure to handle the case of the strings having different lengths.
  3. Now write a method compare(String[], String[]) -> int. This can look very similar to the above (in fact, you could make a generic one for both), but it might be simpler to make this one specific for the "lastname-firstname" case, particularly since here you want to sort by the second element first.
  4. Finally, write your own sort method. An in-place bubble sort should be the easiest and the algorithm can easily be found on the internet. Other sort algorithms are faster, but if speed is an issue, the requirement not to use any API is nonsensical in the first place. If you want to score bonus-points, though, you can try to implement an in-place quick sort, but only after you've got it running with the bubble sort.

Also, you should test each of those methods individually. Don't try to run your sort method before you've made sure your compare methods actually work. Call them individually with different outputs and see whether they yield the correct result.

like image 85
tobias_k Avatar answered Mar 05 '26 14:03

tobias_k


public class NameSort {

    public static void main(String[] args) {

        String [][] names={{"Jen","Eric"},
          {"Brain","Adams"},
          {"Jon","Methew"},
          {"Antino","Ronald"},
          {"Cris","Ronald"}
         };

        for(int m=0;m<names.length;m++)
        {
            for(int n=m+1;n<names.length;n++)
            {
                if(myCompare(names[m][1],names[n][1])==1)
                {
                    swap(names, names[m], names[n], m, n);
                }
                else if (myCompare(names[m][1],names[n][1])==0)
                {
                    if(myCompare(names[m][0],names[n][0])==1)
                    {
                        swap(names, names[m], names[n], m, n);
                    }
                }
            }
        }
        for (int i=0;i<names.length;i++)
        {
            System.out.println(names[i][0]+" " +names[i][1] );
        }

    }

    public static void swap(String [][] names,String[] a,String[] b,int m,int n)
    {
        names[n]=a;
        names[m]=b;
    }

    public static int myCompare(String a, String b)
    {
        int minLength= a.length()<b.length()?a.length():b.length();
        for(int i=0;i<minLength;i++)
        {
            if(a.charAt(i)>b.charAt(i))
            {
                return 1;
            }
            else if(a.charAt(i)<b.charAt(i)){
                return -1;
            }
        }
        if(a.length()>minLength)
            return 1;
        else if (b.length()> minLength )
            return -1;
        else
            return 0;
    }
}
like image 26
PANKAJGSINGH Avatar answered Mar 05 '26 13:03

PANKAJGSINGH