Hi,

I'm pretty new to java and I'm not asking for anyone to do any coding for me. I just need pointing in the right direction.

How do I go about printing out the objects of an array sorted alphabetically by name without altering the original array. I know I can't use the .sort method to achieve this and I don't want to make a new temp array to do this either.

I know I have to use the compareTo method somewhere....

I'm just confused

Any help appreciated
Thanks

More details are needed.

For example, what type of goal are you trying to achieve? Finding a low-running time way of printing your array or just getting the job done?

The Iterative/Recursive approach

T, V, E, D, A, B

T is found
V is found, V should be printed after T
E is found, E should be printed before T
D is found, D should be printed before T, before E
A is found, A should be printed before T, before E, before D
B is found, B should be printed before T, before E, before D but after A

Once all of the indexes in the array are reached, recursively print the results correctly.

I have no idea why you want to do this! What's wrong with sorting a copy? Remember that the copy array, like the original, is simply an array of references (like pointers) to the Strings; you won't copy the actual Strings unless you go out of your way so to do.

More details are needed.

For example, what type of goal are you trying to achieve? Finding a low-running time way of printing your array or just getting the job done?

The Iterative/Recursive approach

T, V, E, D, A, B

T is found
V is found, V should be printed after T
E is found, E should be printed before T
D is found, D should be printed before T, before E
A is found, A should be printed before T, before E, before D
B is found, B should be printed before T, before E, before D but after A

Once all of the indexes in the array are reached, recursively print the results correctly.

Thanks for your quick response.

I am looking for a way of using as little code as possible and using any of the java library classes available for the job.

I have no idea why you want to do this! What's wrong with sorting a copy? Remember that the copy array, like the original, is simply an array of references (like pointers) to the Strings; you won't copy the actual Strings unless you go out of your way so to do.

lol ok ok thats why i obviously need help....

so for example... I have an array of contact items (each consisting of first names, surnames and numbers) and I create a temp array... then how do I sort the temp array in order of their surnames so they can be printed

thanks

Since any Java-API based class is valid for the assignment....

I suggest inserting the values into a B-Tree (Binary Tree) then printing the objects in the tree via In-order traversal.

import java.util.TreeSet;
import java.util.SortedSet;

public class TreeSet_Test{

	public static void main(String... args){

		String values[] = {"T", "V", "E", "D", "A", "B"};


		TreeSet<String> tree = new TreeSet<String>();

		for(String element : values)
			tree.add(element);

		SortedSet<String> result = tree.tailSet("A");

		for(String element : result)
			System.out.print(element + " ");
	}

}

Since any Java-API based class is valid for the assignment....

I suggest inserting the values into a B-Tree (Binary Tree) then printing the objects in the tree via In-order traversal.

I appreciate your help...

but did i mention I was pretty new to java lol

ok... back to basics

I would just like to sort my arraylist of contacts alphabetically by name.
I have different types of contacts... with contact being the superclass (and is also an abstract class). I have also tried to create a comparable interface... but when I come to implement it I get an error... something to do with my abstract class.

I'm getting really confused now... I just thought it would be a simple process...

Create a copy of your array and sort it using the built-in sort method, but with a custom Comparator...
define your own comparator (see the API) for your Contact class, which calculates its results by simply comparing the surnames.
See http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-sort.html?page=2 for more details

Thanks... i'll have a look now... you must have read my mind... I was posting my message before I saw yours

I'm getting really confused now... I just thought it would be a simple process...

There's no problem with an abstract superclass, as long as the superclass defines (but not necessarily implements)the public methods that you need for the comparator.
It may seem confusing while you're still getting into it, but when it's done and you look back you'll see how easy it was really:
1. define comparator (2 1-line methods)
2. define new arraylist as copy of original (1 short line)
3. call sort (1 short line).
Don't Panic

The most important advice you can remember when you're learning is not to get overwhelmed, and break your task into smaller parts, like James has suggested. That way, you can look up how to do each individual part, if necessary, and you can make progress on the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.