6 Replies - 6246 Views - Last Post: 12 April 2007 - 07:38 AM Rate Topic: -----

#1 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Sorting Arrays of Strings alphabetically

Posted 11 April 2007 - 09:41 PM

I'm trying to create a method that will sort an Array of Strings alphabetically and create a new array that is ordered with way while leaving the original Array unchanged.

I'm not entirely sure how to even begin, but so far I have:

public String[] sort()
	{
		String min;

		String[] tmp = new String[mySize];

		for (int i=0; i<mySize; i++)
		{
			for(int j=0; j<mySize; j++)
				if (list[j].compareTo(list[i])<0)


I'm not sure where to go from there, or even if that's right. I want to do an insertion sort where it reads each string in the array, then sets the one with the lowest value to a position in the array, increment that position, then do it all again. I'm totally lost though.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Arrays of Strings alphabetically

#2 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 12:06 AM

I'm not all that familiar with Java, but perhaps the answer lies within java.util.Arrays.sort ? Here's a couple Google results that appeared relevant:

TJDA 1.4 - Sorting an Array

Sun - java.util Class Arrays

2
Was This Post Helpful? 0
  • +
  • -

#3 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 01:03 AM

You might also want to check out the code snippet that was just recently added:

Array Sorting
Was This Post Helpful? 0
  • +
  • -

#4 thilka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-April 07

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 01:06 AM

Hello,

first of all you should copy your array using System.arraycopy(...).

Then you can sort the array using Arrays.sort(...).

This will sort your array in ascending order.

If you need some special ordering of your array, you can write your own Comparator (see java.util.Comparator).

Tobias
Was This Post Helpful? 0
  • +
  • -

#5 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 01:53 AM

Are you allowed to use that sort method, or have you been asked to write your own sorting method?
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 03:33 AM

Since you need to leave the old array unchanged, the first think you need to do is create a new array that is the same size of you old array.

String newArr[] = new String[oldArr.length];

And I gather that, since you've specified insertion sort, you have to implement your own sorting algorithm. You can either copy all of your values to the new array and then sort, or you can sort as you go. If you choose to sort as you go, each time you put a new string into your new array, do an insertion sort. Here's a pseudo-code example:

oldArray: { "f", "a", "z", "c" }
newArray: {null, null, null, null}

insert "f into newArr
oldArray: { "f", "a", "z", "c" }
newArray: {"f", null, null, null}
				  ^

"f" is at top so sorting stops

insert "a" into newArr
oldArray: { "f", "a", "z", "c" }
newArray: {"f", "a", null, null}
					   ^
does "a" come before "f"?  Yes, so "swap them:
newArray: {"a", "f", null, null}
				  ^
"a" is at top, so sorting stops.

insert "z" into newArr
oldArray: { "f", "a", "z", "c" }
newArray: {"a", "f", "z", null}
							 ^

does "z" come before "f"?  No, so leave it and sorting stops.

insert "c" into newArr
newArray: {"a", "f", "z", "c"}
								   ^

does "c" come before "z"?  Yes, so swap them
newArray: {"a", "f", "c", "z"}
							 ^

Does "c" come before "f"?  Yes, so swap them
newArray: {"a", "c", "f", "z"}
						^

Does "c" come before "a"?  no, so leave it and sorting stops.
newArray: {"a", "c", "f", "z"}

No other values to insert, so you're done.



Check out the String compareTo and compareToIgnoreCase methods. You'll need to use one of these to determine whether a string comes before or after another string.
Was This Post Helpful? 0
  • +
  • -

#7 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Sorting Arrays of Strings alphabetically

Posted 12 April 2007 - 07:38 AM

Great, thanks, that helps a lot. I'll see what I can get based off of that!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1