Sorting Strings in an Array

Sorting Strings in an Array

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?

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 conjunctionwith 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 areworking 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. Sortingis a well-studied area, and you will find many algorithms to choose from by visiting your local library or bookstore’s computer algorithms section.

See also  Why ChatGPT Is So Important Today

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist