How do i sort a string array in alphabetical order?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaifaCarina
    New Member
    • Sep 2007
    • 13

    How do i sort a string array in alphabetical order?

    these are the loops i used:

    Code:
    for (h = 0; h<name.length;h++)
              {
                highestOrder [h] = Integer.valueOf(name[h].charAt(0));
             	highestName [h] = name[h];
                for (int count = 0; count<name.length;count++) {
    
    	            if (Integer.valueOf(name[count].charAt(0)) < highestOrder[h])
    	            {
    
    	                highestOrder[h] = Integer.valueOf(name[count].charAt(0));
    	                temporary = Integer.valueOf(name[h].charAt(0));
    	                
    	                Integer.valueOf(name[h].charAt(0))= Integer.valueOf(name[count].charAt(0));
    	                Integer.valueOf(name[count].charAt(0))= temporary; 
    	                	
    	                
    	                highestName[h] = name[count];
    	                tmp = name[h];
    	                name[h] = name[count];
    	                name[count] = tmp;
    	
    	            }
    	         }
              }
    	     for (int count=0; count<name.length;count++)
             {
               	 System.out.println(highestOrder[count] + " ");
                 System.out.println(highestName[count] + " ");
             }
    here's the part with the error:
    Code:
    	               
    temporary = Integer.valueOf(name[h].charAt(0));
    Integer.valueOf(name[h].charAt(0))= Integer.valueOf(name[count].charAt(0));
    Integer.valueOf(name[count].charAt(0))= temporary;
    and i can't find a way how to substitute it or anything..or is there any simplified way to sort string arrays in alphabetical order? i really need help...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Better use the Arrays.sort() method; or do you have to sort the elements in
    your array yourself? Is it homework?

    kind regards,

    Jos

    Comment

    Working...