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 2 of 2

Thread: Help With Sorting Multidimensional String Array

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Sorting Multidimensional String Array

    I am new to Java, but to expand my knowledge of it. Below is a class assignment that I'm having a very, very difficult time completing. I need to create a method that passes in a two dimensional string array and then sorts the array by the second element.

    Method sortContacts()

    o Declared as a public static method, it should take a two-dimensional String array as a parameter,
    and an integer value for the number of contacts in the array
    o Returns nothing
    o Sorts the contents of the contact list given as a parameter using the lastName as the sorting
    field
    o You may use any of the sorting mechanisms described in the text (bubble, insertion or selection)
    o Key concept: as you implement the sort, the trick is to identify the dimension for the values you
    are comparing and how they relate to the values from your loops.
    o Hints:
     Do not rely on the array’s length for your loops, instead use the value from the number
    of contacts
     Your temp variable will be an array of size 3, String[] temp = new String[3];
     You will need to use the string1.compareTo(string2) method to compare two strings.

    Sample data: (first name, last name, phone number)

    String [][] contactsArray = {

    {"Emily","Watson","913-555-0001"},
    {"Madison","Jacobs","913-555-0002"},
    {"Joshua","Cooper","913-555-0003"},
    {"Brandon","Alexander","913-555-0004"},
    {"Emma","Miller","913-555-0005"},
    {"Daniel","Ward","913-555-0006"},
    {"Olivia","Davis","913-555-0007"},
    {"Isaac","Torres","913-555-0008"},
    {"Austin","Morris","913-555-0009"}


     
    public static void sortContact(String[][] contactsArray, int numContacts) {
    		// TODO Auto-generated method stub
     
     		String[] temp = new String[3];
     		String [] words = contactsArray[3];
     
     		for(int i = 0; i < numContacts-1; i++)
     		{
     		    int smallest = i;
     		    for(int j = i + 1; j < numContacts-1; j++) /* here you find the index of the minimum String between 
     		    the strings in the unsorted side of the array*/
     		    {
     
     		    	//Prints loop variables so I can see them before any action
     		    	System.out.println("First: "+words[j]+" " +"Second "+words[i]);
     		        if(words[j].compareTo(words[i]) < 0)
     		            smallest = j;
     		    }
     
     		   //put the new minimum in the i-th position.
     
     		    //String temp = words[i];
     		    words[i] = words[smallest];
     		    words[smallest] = temp[i];
     		} 	
    	}

    I'm not sure where to put the temp array

    There are tons of examples for integer arrays but not a lot for string arrays,

    Any suggestions are appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Sorting Multidimensional String Array

    What is wrong with the posted code? Can you print out the output from the method so we can see it?
    Use the Arrays class's deepToString method as an easy way to print the contents of a 2 dim array.

    I'm not sure where to put the temp array
    What is the temp array supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multidimensional array uses
    By SunshineInABag in forum Java Theory & Questions
    Replies: 2
    Last Post: April 13th, 2024, 03:57 AM
  2. Sorting objects of an array by String name
    By esbowman in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 13th, 2013, 12:47 AM
  3. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  4. [SOLVED] adding to a multidimensional array.
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 10:09 AM
  5. Need help in multidimensional array
    By Stefan_Lam in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 14th, 2010, 08:52 PM