| Help with sorting array! 
 
	 
	
		| Author | Message |   
		| an_mui 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 06, 2005 12:18 am    Post subject: Help with sorting array! |  |   
				| 
 |  
				| Hi I am kind of new to programming, and I have to make a program that will keep track of an user's collection.  I need help on making a menu that will allow the user to choose what to do next. I know first I have to accept input from user, and then user ifs and else ifs? Any help is appreciated 
 
 - Annie
 
 EDIT (Martin): Just changed the tags to syntax ones to try out the new system. Carry on.
  |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 06, 2005 12:48 am    Post subject: (No subject) |  |   
				| 
 |  
				| In psuedo-code: 
 
 
 Welcome
Display menu.
Accept input.
If input is not one of the choices, go to step 2.
Test to see what the input is, and do different things depending on that. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| an_mui 
 
 
 
 
 | 
			
				|  Posted: Mon Jan 10, 2005 9:18 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Thanks, the help is appreciated =) |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| an_mui 
 
 
 
 
 | 
			
				|  Posted: Tue Jan 11, 2005 5:08 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Sorry but I've encountered any problem. 
 For my program, I have 4 arrays to store four types of data that the user entered. The tutorials provided on this web site only deal with sorting one array. However.. for my colleciton, if I sort one array, then I must sort the other array in the same order.. or else the data won't match up.
 
 Sorry if I am not explaining myself clearly.. here's what I meant
 
 Sample Input:
 
 1: 1998, eric, tang
 2. 2000, chris, boyd
 3. 2003, stephanie, mac
 
 If i sort according to first name alphabetically, then Chris would become associated with 1998, and tang.. which is not true. How would I make my program so that if i sort last name / first name alphabetically.. the other entries would also move accordingly?
 
 Thank you for the help
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Tue Jan 11, 2005 6:09 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Don't try to sort four arrays in exactly the same way.  Simply have one array of objects.  You can create a class which holds all of your data.  Let's say I have people, and I want to store their names and ages. 
 Now, I can create a Person class.  If I implement the Comparable interface, I'll be ably to sort my array using the Arrays.sort method.
 
 
 	  | Java: |  	  | import java.lang.*;
import java.io.*;
import java.util.*;
class Person implements Comparable
{
   private String  name;
   private int  age;
   public  Person(String  name, int  age)
   {
      this .name  = name;
      this .age  = age;
   }
   public String  getName()
   {
      return  name;
   }
   public int  getAge()
   {
      return  age;
   }
   public int  compareTo(Object  o)
   { 
      Person p = ( Person) o;
      int  comparison = name.compareTo( p.getName()) ;
      if ( comparison == 0)
      {
         int  otherComp = new Integer( age) .compareTo(new Integer( p.getAge())) ;
         return  otherComp;
      }
      else
      {
         return  comparison;
      }
   }
   public String  toString()
   {
      return  name + " "  + age;
   }
}
public class  SortTest
{
   public static void  main(String[]  args)
   { 
      Person[]  people = new  Person[4] ;
 
      people[0]  = new  Person("Bob" , 45) ;
 
      people[1]  = new  Person("John" , 23) ;
 
      people[2]  = new  Person("Arnold" , 18) ;
 
      people[3]  = new  Person("Bob" , 16) ;
      Arrays .sort( people) ;
      for (int  i = 0 ; i < 4 ; i++)
         System .out .println( people[ i]) ; 
   }
} | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| an_mui 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 13, 2005 8:09 pm    Post subject: (No subject) |  |   
				| 
 |  
				| hm.. I've been havnig difficulties sorting string.. can you help me once more? |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 13, 2005 8:26 pm    Post subject: (No subject) |  |   
				| 
 |  
				| If you're just sorting an array of strings, that's incredibly easy. 
 
 	  | Java: |  	  | String[] foo = {"hello" ,"world" ,"foo" ,"bar"} ;
Arrays .sort( foo) ; | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Nezura 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 20, 2005 12:48 am    Post subject: (No subject) |  |   
				| 
 |  
				| wtd wrote: If you're just sorting an array of strings, that's incredibly easy.
 	  | Java: |  	  | String[] foo = {"hello" ,"world" ,"foo" ,"bar"} ;
Arrays .sort( foo) ; | 
 With Arrays.sort  does it sort the foo array ...but doesn't output it does it?...how will we make it output the new sorted array?...loops?
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Thu Jan 20, 2005 1:45 am    Post subject: (No subject) |  |   
				| 
 |  
				| Nezura wrote: wtd wrote: If you're just sorting an array of strings, that's incredibly easy.
 	  | Java: |  	  | String[] foo = {"hello" ,"world" ,"foo" ,"bar"} ;
Arrays .sort( foo) ; | 
With Arrays.sort  does it sort the foo array ...but doesn't output it does it?...how will we make it output the new sorted array?...loops?
 It just sorts the array.  Output the array the same way you would normally.  Yes, with a loop.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |