JAVA problem array sorting

09/28/2012 06:39 morphling18#1
i use three arrays where in
System.out.println("Enter number of process: ");
int process = in.nextInt();
String name[] = new String[process];
int age[] = new int[process];
double income[] = new double[process];

bla bla bla so basically i used three arrays and a for loop and the outcome is this.
(note: this is a user input)

so here's my inputs
================
|| Name || Age || Income ||
================
|| chuck || 40 || 40,000 ||
|| norris || 65 || 32,000 ||
|| nemo || 14 || 37,200 ||
================

what should i do if i want it to sort like this?

================
|| Name || Age || Income ||
================
|| nemo || 14 || 37,200 ||
|| chuck || 40 || 40,000 ||
|| norris || 65 || 32,000 ||
================

i want to sort it by age from the youngest to the oldest
and still the name and income is still associated with the age.

can somebody help me ?
09/28/2012 09:08 Obilee#2
Code:
import java.util.Arrays;
import java.util.Scanner;


public class Main {

	private static class Person implements Comparable<Person> {
		
		private String m_name;
		private int m_age;
		private double m_income;
		
		public Person(String name, int age, double income) {
			this.m_name = name;
			this.m_age = age;
			this.m_income = income;
		}
		
		public String getName() {
			return m_name;
		}
		
		public int getAge() {
			return m_age;
		}
		
		public double getIncome() {
			return m_income;
		}

		//compares this object with a other object for the sorting
		@Override
		public int compareTo(Person p) {
			if( m_age < p.getAge() )
	            return -1;
	        if( m_age > p.getAge() )
	            return 1;
	            
	        return 0;
		}
		//to allow a "nice" output string
		@Override
		public String toString() {
			return m_name + " " + m_age + " " + m_income;
		}
	}
	
	public static void main(String[] args) {
		
		//Array with all Persons
		Person[] persons = new Person[3];
		//Input Scanner
		Scanner input = new Scanner(System.in);
		//User Input ( do it in your Loop)
		for (int i = 0; i < 3; i++) {
			String name = input.next();
			int age = input.nextInt();
			double income = input.nextDouble();
			//Create new Person and save him
			Person person = new Person(name, age, income);
			persons[i] = person;
		}
		//output unsorted array
		System.out.println(Arrays.toString(persons));        
		//sort
		Arrays.sort(persons);		
		//output sorted array
		System.out.println(Arrays.toString(persons));

		
	}
}
I don't know how far your level is but this is a quick solution. You need to put your information about the persons in a Person object. With this you have them always "together" and your problem that you cant refer the age with the right name is gone.

Arrays offers a method to sort all object in them, for this you need the compareTo method in your Person object which says if this person is younger or older than the other person.

try it and ask questions.

Code:
int[] array = new int[]{ 4, 6, 2, 7, 5, 8, 9, 3, 1 };
System.out.println(Arrays.toString( array ));
java.util.Arrays.sort(array);        
System.out.println(Arrays.toString( array ));
Would be also possible if you dont want to use objects, but here you have to asign the names and incomes to the ages what is rly a ugly thing to do.

if you dont want to do it with the arrays.sort u need to read about sort algorithms and implement them yourself: http://en.wikipedia.org/wiki/Sorting_algorithm