Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: String Manipulation
Expertise: Beginner
Dec 4, 1998

Sorting Strings in an Array

Question:
I have a list of filenames in a String array. I'd like to sort them into alphabetical order. Is there a class to do this, or must I implement my own sort?

Answer:
If you are using JDK 1.2, you're in luck and won't have to roll any of your own code. The java.util.Arrays class in conjunction with the Collections API provides a ready-made way of sorting arrays. Keep in mind though that the sorts in the Arrays class are designed to work well with a large range of data sets, but may still not necessarily work well with the specific data distribution you are working with. But for the case of sorting a bunch of filenames, the provided sorting algorithms will no doubt be more than adequate. Using JDK 1.2, you could sort an array of filenames as simply as this:

    String[] stringArray;
    ...
    java.util.Arrays.sort(stringArray);

But if you are not using JDK 1.2 yet, you will have to roll your own sort or use a third-party library such as ObjectSpace's JGL. Sorting is a well-studied area, and you will find many algorithms to choose from by visiting your local library or bookstore's computer algorithms section.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date