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: ascending sort

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default ascending sort

    Hi, i'm having problems making the sort be in ascending order instead of descending order.
    public static void main(String[] args) throws IOException, FileNotFoundException {
          int[] intArray = processFile("c:\\scores.txt");
          sortArray(intArray);
            writeToFile ("c:\\scores.txt",intArray);    
        }
     
        public static int[] processFile (String filename) throws IOException, FileNotFoundException 
    	{
                    int []intArray = new int[25];
                    int counter = 0;
    		int number;
    		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    		String line;
     
    		while((line = inputReader.readLine()) != null)
    		{
    			number= Integer.parseInt(line); 
                            intArray[counter] = number;
                            counter++;
     
    		}		
    		inputReader.close(); 
                    return intArray;
    	}    
     
        public static int indexOfMaxInRange(int[] arr, int lowIndex, int highIndex) {
    	int indexOfMax = lowIndex;
     
    	for (int i = lowIndex+1; i <= highIndex; i++) {
    		if (arr[i] > arr[indexOfMax]) {
    			indexOfMax = i;
    			}
    		}
    		return indexOfMax; 
    	}
     
          public static void swapElement(int[] arr, int index1, int index2) {
    	int tempv = arr[index1];
    	arr[index1] = arr[index2];
    	arr[index2] = tempv; 
    	}
     
        public static int[] sortArray (int[] a) {
        for (int i=0; i>a.length; i++) {
          int j = indexOfMaxInRange (a, i, a.length-1);
          swapElement (a, i, j);}
            return a;
    	}
     
     
         public static void writeToFile (String filename, int[] Array) throws IOException {  
            BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
            int Num;
     
                for (int i = 0; i < 25; i++) {
                   outputWriter.write(Integer.toString(Array[i]));
                   if(i!=24) outputWriter.newLine();
     
                }
     
            outputWriter.flush(); 
            outputWriter.close();
    }
    }
    this is the output
    25
    24
    ...
    2
    1
    but i want it to be 1-25 in order....


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: ascending sort

    i'm having problems making the sort be in ascending order instead of descending order
    What - in your words - does the sortArray() do, and how does it do it?

Similar Threads

  1. ascending order array
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:41 PM
  2. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  3. organizing linkedlist in ascending order
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 9th, 2012, 06:44 AM
  4. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM