Results 1 to 7 of 7
Thread: Help with my sort program
- 07-21-2010, 06:50 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Help with my sort program
I need help on this sorting I don't really know how to sort 2d arraysJava 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]; } } } }
- 07-21-2010, 08:31 PM #2
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
- 07-22-2010, 12:30 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
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)
- 07-22-2010, 12:35 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
But what way do you want to sort the students? by marks, by name, by what?
- 07-22-2010, 12:41 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
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
- 07-22-2010, 12:45 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
By what source are you reading students into your program, by file or by manual input, or predefined in your program.
- 07-22-2010, 12:50 AM #7
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:
Repeat this for the gender, and you should be done.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]
if you dont understand why that should work, please ask :)
berkeleybross
Similar Threads
-
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
Bogo Sort
By Zosden in forum AlgorithmsReplies: 0Last Post: 04-29-2008, 08:07 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
sort
By Camden in forum New To JavaReplies: 7Last Post: 11-28-2007, 01:11 AM -
how to sort
By Feng in forum New To JavaReplies: 1Last Post: 11-20-2007, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks