13 Replies - 25886 Views - Last Post: 08 March 2009 - 06:00 PM Rate Topic: -----

#1 onurevitan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 onurevitan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 onurevitan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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




I am slighly confused on using it with Strings.. can you please help me?

Thanks..

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

Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:15 PM

please edit your post and place code tags around your code
[ code ] your code [ /code] (without the spaces in the tags)
Was This Post Helpful? 0
  • +
  • -

#7 onurevitan  Icon User is offline

  • New D.I.C Head

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

Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 03:17 PM

Okay, Edited.
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#10 onurevitan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#11 onurevitan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Sorting in Alphabetical Order

Posted 08 March 2009 - 06:00 PM

You already swapped int
swapping String will be the same process whith 2 differences:
- the method signature will be (String[, int, int) instead of (int[], int, int)
- temp will be String instead of a int

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1