• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alphabetic Sorting in various data structures.

 
Reid Hattaway
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Reid.
I ran across this page, which says:

Sort a String array

Sort utilities are now part of latest JDK versions.
Case sensitive:
java.util.Arrays.sort(myArray);

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

Hope this helps,
Art
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reid:
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
Detlev
 
Reid Hattaway
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your replies and the time spent on them. The information provided was exactly what I need!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic