1 Replies - 5060 Views - Last Post: 28 January 2011 - 07:23 PM Rate Topic: -----

#1 sampsoni  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-October 09

Sorting number and String arrays in descending order and alphabetical

Posted 28 January 2011 - 07:18 PM

I'm trying to complete my first homework assignment in Programming Principles #2 and I'm trying to make a class that will sort two different arrays. A double value array will be arranged in descending order, while a String value array will be arranged in alphabetical order. My professor gave me a hint, and I need some help for the code I'm about to post because I think I made a few errors. He said I could copy a bit of code from my book and then modify it a bit to make it work. I just have to reverse it from ascending to descending. Here's my code:

public class GradeSort {
	private String[] names;
	private int[] scores;
	
	public void sort(String[] theNames, int[] theScores){
		
		names = theNames;
		scores = theScores;
		
		for(int i = 1; i < theScores.length; i++){
			double number = theScores[i];
			int numberindex = i;
			for(int j = 0; j < theScores.length + 1; j++){
				if (number > theScores[j]){
					number = theScores[j];
					numberindex = j;
				}
			}
			if(numberindex != i){
				theScores[numberindex] = theScores[i];
				theScores[i] = (int)number;
			}
		}
	}
}



My IDE, Eclipse, is also giving me a few incompatible data types and operators. Can you help me correct those?

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting number and String arrays in descending order and alphabetical

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Sorting number and String arrays in descending order and alphabetical

Posted 28 January 2011 - 07:23 PM

So loop it from array.length-1 to 0, i--, each time and instead of shifting backwards, shift forwards.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1