aspose file tools
The moose likes Beginning Java and the fly likes Alphabetic Sorting in various data structures. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Alphabetic Sorting in various data structures." Watch "Alphabetic Sorting in various data structures." New topic

Alphabetic Sorting in various data structures.

Reid Hattaway

Joined: Jul 15, 2001
Posts: 7
I've done most of my programming in C++ and Python and I have been impressed w/ all of the built in classes and methods of Java as I have started to learn it. However, I can't seem to find the function or functions that I need to finish my task. I would like to be able to sort Strings, Vectors, or Arrays into alphabetical order (w/o having to write my own functions!). Given a string "ABCA", i would like the function to return a new string "AABC". Given a vector or array of Strings, i would like the function to alter the same array or vector so that its components are sorted by their String values. Is this possible, or do I need to write this function?
I would also like to know if Java has any pre-built algorithms such as a Binary Search.
I do not have a Java book yet, so I only have Sun's online documentation.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Reid.
I ran across this page, which says:

Sort a String array

Sort utilities are now part of latest JDK versions.
Case sensitive:

Case insensitive:
java.util.Arrays.sort(myArray, String.CASE_INSENSITIVE_ORDER);

Hope this helps,
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
1.) About the algorithms:
On all Collections (the most important: ArrayList & Vector, but there are more), you have implemented the following algorithms: sort, binarySearch, reverse, shuffle, fill, copy, min, max. These are static methods in the Collections.class (which just offers these algorithms on Collections).
2.) About the sorting of a String:
String are /not/ implemented as an array of chars. /But/ they are somehow equivalent, so you can convert a string to an array of chars and vice versa. With this, you have the following solution on your problem:

All this can be found in the API Specification (you can use the online-search at sun!).
Hope it helps
Reid Hattaway

Joined: Jul 15, 2001
Posts: 7
Thanks all for your replies and the time spent on them. The information provided was exactly what I need!
I agree. Here's the link:
subject: Alphabetic Sorting in various data structures.