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 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS