# Sorting in Alphabetical Order

Page 1 of 1

## 13 Replies - 30930 Views - Last Post: 08 March 2009 - 06:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=91433&amp;s=bb8a5eef5bf39f0bbe454e2734e4c56d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

# Sorting in Alphabetical Order

Posted 08 March 2009 - 02:29 PM

Hello Guys,

I am facing with some problems on creating a code for Sotring in Alphabetical Order..

I am a 1st year student in uni.. I am supposed to sort this question out.. -> 16.3) It has been decided that a facility is needed to print to the screen a list of student names to be used for registers. This list must be sorted alphabetically by student name. Write an efficient sort program that when requested prints to the screen such a sorted list of names.

Can anybody help me to create a code for this one? I actually need the code of converting String's first character into an ASCII code.. for Example, user inputed Onur and I just would like to get O in order to convert it as a character into an ASCII code.. or.. If you have a better solution of doing this, please share with me..

Thanks..

Is This A Good Question/Topic? 0

## Replies To: Sorting in Alphabetical Order

### #2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 02:33 PM

this can be easily done using a sorting algorithm and the compareTo() method...

Quote

String's first character into an ASCII code.. for Example, user inputed Onur and I just would like to get O in order to convert it as a character into an ASCII code.

you dont need all that

### #3 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 02:35 PM

How can I do it? I've been told by my friends to do it from that way.. but some of my friends told me who don't know java.. java might have a simple code to make you able to compare the things alphabetically..

### #4 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 02:46 PM

as for the sorting algorithm, i would recommend the selection sort for you since it is the easiest.. as for comparing strings
```string1.compareTo(string2);

```

the above will return 0 if both strings are equal, and will return a negative integer if string1 lexicographically precedes string2 and will return a positive integer if string2 precedes string1 lexicographically...you can check out my profile, you will find a snippet for the selection sort that i wrote with a brief description of how it works..you will just need to do a few modifications to make it work with strings.. good luck

### #5 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:08 PM

Hi again,

Thank you very much for your time and help.

string1.compareTo(string2); This helped me alot actually but I am stucked logically. I know how to use sorting and etc.. This was the only thing that I was wondering.. Please take a look the code below;

```String str[] = {"Certan", "Amca", "Zila", "Onur"};

for (int i=0; i<4; i++)
{
str[i].compareTo(str[i+1]);
}
```

I will compare the Strings using this code than I will improve it in the real program using 2 for loops. However, the thing is I will need to swap these things use sorting. I only know how to use Sorting with integers..

I was using this code in order to swap them;

```public static void bubble_sort(int array[], int arraySize)
{ // Bubble Sort Method
for (int i=0; i<arraySize-1; i++)
{ // For Loop B
for (int c=0; c<arraySize-1; c++)
{ // For Loop C
if (array[c]>array[c+1])
{ // If 1
swap(array,c,c+1);
} // If 1 Ends
} // For Loop C ends
} // For Loop B ends
} // Bubble Sort Method

public static void swap(int a[], int n1, int n2)
{ // Swap Method

int temp;
temp=a[n1];
a[n1]=a[n2];
a[n2]=temp;
} // Swap Method ends
```

By the way the Full Code is below;

```import javax.swing.*;
import java.util.*;

public class Unit16_2
{ // Class Starts
public static void main(String[]args)
{ // Main Method Starts
final int SIZE = 4;
int numbers[] = {4,2,3,1};

fill_array (numbers, SIZE); // Filling The Array With the approciate values
print_array (numbers, SIZE); // Printing array with the values that are inputed.
bubble_sort (numbers, SIZE); // Calling Bubble Sort Method
print_array (numbers, SIZE); // L4 - Printing the Array with Bubble Sorting Method.
System.exit(0);

} // Main Method Ends

public static void fill_array(int array[], int arraySize)
{ // Fill Array Method
for (int i=0; i<arraySize; i++)
{ // For A
array[i]=Integer.parseInt(JOptionPane.showInputDialog("Please Enter Number for position: " + i));
} // For A ends
} // Fill Array Method Ends

public static void bubble_sort(int array[], int arraySize)
{ // Bubble Sort Method
for (int i=0; i<arraySize-1; i++)
{ // For Loop B
for (int c=0; c<arraySize-1; c++)
{ // For Loop C
if (array[c]>array[c+1])
{ // If 1
swap(array,c,c+1);

} // If 1 Ends
} // For Loop C ends
} // For Loop B ends
} // Bubble Sort Method

public static void swap(int a[], int n1, int n2)
{ // Swap Method

/* Temp -> Number One
Number One -> Number Two
Number Two -> Temp (Number One)

This progress completes the swapping. */

int temp;
temp=a[n1];
a[n1]=a[n2];
a[n2]=temp;
} // Swap Method ends

public static void print_array(int array[], int arraySize)
{ // Print Array Method
for (int i=0; i<arraySize; i++)
{ // For Loop D
System.out.print(array[i] + "\t");
} // For Loop D Ends
System.out.println();
} // Print Array Method Ends
} // Class Ends
```

Thanks..

This post has been edited by onurevitan: 08 March 2009 - 03:19 PM

### #6 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:15 PM

[ code ] your code [ /code] (without the spaces in the tags)

### #7 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:17 PM

Okay, Edited.

### #8 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:17 PM

```for (int i=0; i<4; i++)
{
str[i].compareTo(str[i+1]);
}

```

One thing that you will have a problem with is that when it gets to the last elemnt in the array, it will try to call something that doesn't exist. You will get an error OutOfBoundsError.
If i were making this program i would make an arraylist instead of arrays. That way you will compare 2 items, if the String2 is before string1, then you can get the index of string 1, and set string 2 to that index so it just moves String1 over 1 element to the right and set String2 before String 1. Thats the easiest and most efficient way i think

Edit - Also You don't want to use a Magic Number in the condition of the for loop. Change the i<4 to i<arraySize;

This post has been edited by Fuzzyness: 08 March 2009 - 03:26 PM

### #9 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:17 PM

```if (array[c]>array[c+1])

```

since you are sorting strings, you cant use these operators..this is where teh compareTo() method comes in handy...

### #10 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:26 PM

Yeah.. it is where I exactly confused..

### #11 onurevitan

Reputation: 0
• Posts: 18
• Joined: 08-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:39 PM

To be honest, I do not know how to use CompareTo() method.. that is why I am suffering on creating a program as I supposed... ;() Can you help me to create one using compareTo method?

### #12 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:49 PM

Won't create the code for you but i will help you understand it. If you use compareTo() method, it returns an int, 0 if it is equal. -1 String1 is before string2, and 1 if String2 is before String1. then use and if statement, if(string1.compareTo(string2) == -1) then the command you want it to be. then just use and else statement for 0, and another else statement for 1.

This post has been edited by Fuzzyness: 08 March 2009 - 03:58 PM

### #13 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:54 PM

i already explained to you how the compareTo() method works in my second post

### #14 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 06:00 PM