Results 1 to 7 of 7
  1. #1
    kneil is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default Help with my sort program

    Java Code:
     public void sortStudent(){
        	String []temp = new String[getStudentLength()];
    //data[i][1] holds the names of the students
        	for(int i = 0; data[i][1]!=null && i < getStudentLength(); i++){
        		temp[i] = data[i][1]+"";
        		
        	}
           Arrays.sort(temp);
    //after sorting the single arrays I implemented it in 2d arrays but I am having trouble doing it
           for(int x = 0; x<temp.length;x++){
           		for(int y =0; y<temp.length;y++){
           	   	if(temp[x].equalsIgnoreCase(data[y][1]+"")){
           	   		data[x] = data[y];
           	   		
           	   		}
           		}
           }
     }
    I need help on this sorting I don't really know how to sort 2d arrays

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Could you give us a better description of your problem?

    At the moment you have just thrown us some code and said you need to sort it.

    What sorting method do you wish to use? Bubblesort, mergesort, binarysort?

    Berkeleybross

  3. #3
    kneil is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    I guess any sorting algo will do I don't really care much bout the running time
    data[i] holds a student
    data[i][1] holds the name of the student

    so if I want to sort this I want to sort them using data[i][1]

    I tried adding data[i][1] to a single array callled temp

    I used Arrays.sort(temp) to sort temp which are the names of the student

    now I am having trouble relating the temp to my data!!!

    If you have anything better can you please tell cause I don't really know how to sort 2d arrays(actually Idk how to sort singles to)

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    But what way do you want to sort the students? by marks, by name, by what?

  5. #5
    kneil is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    I want to sort them by names no marks just start with the first word..., actually what I really need is to seperate them first from male and female and then sort the males and then sort the females

    data[i][4] actually holds their gender
    data[i][1] holds names

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    By what source are you reading students into your program, by file or by manual input, or predefined in your program.

  7. #7
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    To sort by multiple field you can do one of two things.
    You can sort multiple times, the least important first. So in your case you would sort by names and then sort by gender.
    Alternatively, you can implement a Comparator which checks if they are the same gender and then by name.

    The advantage of the comparator is you can use it on a 2d array in one sort. You may want to look at this link.

    However I think that is the easy way out if you are trying to learn sorting for the first time.

    Working with what you have, it shouldnt be too difficult to get the sorted result.

    I would suggest you make a new 2d array, which will hold the sorted results.

    With the sorted single array of names, temp, and the 2d array, result:

    Java Code:
    for i...
        temp[i] = data [i][1]
    sort (temp)
    
    for i...
        for j...
            if temp[i] == data[j][1]
                 result[i] = data [j]
    Repeat this for the gender, and you should be done.

    if you dont understand why that should work, please ask :)

    berkeleybross

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  2. Bogo Sort
    By Zosden in forum Algorithms
    Replies: 0
    Last Post: 04-29-2008, 09:07 PM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  4. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 02:11 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 07:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •