Lewis D.
—You are faced with an unsorted array of strings you need to sort lexicographically (or alphabetically). How can you do this?
Let’s take a look at a couple of approaches one can take to solve this sorting setback.
String.compareTo()
The first approach we will consider is programmatically without resorting to any built-in sorting methods. In this approach, our algorithm is simple: Given our String
array, we will iterate over the values twice. Our first loop selects the first element in the array, our second loop goes through the remaining elements and compares each element with our initial string. To make this comparison, we will use the String.compareTo()
method. The compareTo(String string)
method will return a value less than 0
if the string being compared to is lexicographically less than the string passed in. Conversely, the method will return a value greater than 0
if the string being compared to is lexicographically greater than the string argument passed in. If the strings are equal, compareTo()
will return 0
. Using these properties, we can decide on the ordering of the array. Let’s look at how we will sort the array in the example below:
public class Sort { public static void main(String[] args) { String[] array = {"shrew", "porcupine", "newt", "chameleon"}; String[] sorted = sortArray(array); for (String s : sorted) { System.out.print(s + " "); } } public static String[] sortArray(String[] array) { // our outer loop starts at index 0, the first word in the array for (int i = 0; i < array.length - 1; i++) { // our inner loop starts at the next word in the array for (int j = i + 1; j < array.length; j++) { // lexicographically compare the outer string with the remaining strings if (array[i].compareTo(array[j]) > 0) { // if our outer string is lexicographically bigger, swap the string positions String swap = array[i]; array[i] = array[j]; array[j] = swap; } } } return array; } }
Executing the code above yields:
chameleon newt porcupine shrew
One thing to note about our self-coded approach is that this algorithm is not very efficient. This is because of our nested for loop, which means we have effectively coded a Bubble Sort solution, giving us a worst-case time complexity of O(n2). Let’s look at a more efficient (and simpler) approach next.
Arrays.sort()
The simplest way to solve sorting an array is to use the Arrays.sort()
method. This method works for sorting both primitive and object arrays in Java. Because strings are objects, the method we will use is Arrays.sort(Object[])
. This sorting uses the TimSort algorithm, which has a worst-case time complexity of O(n*log(n)). By default, sort()
will sort the array lexicographically. Let’s take a look at an example:
import java.util.Arrays; public class Sort { public static void main(String[] args) { String[] array = {"shrew", "porcupine", "newt", "chameleon"}; Arrays.sort(array); for (String s : array) { System.out.print(s + " "); } } }
Executing the code above yields:
chameleon newt porcupine shrew
As you can see, the built-in Arrays.sort()
method is a simple, readable, and efficient way of sorting an array, and in most cases it will be perfectly adequate for any sorting requirements. However, this answer only considers two types of sorting algorithms. In reality, the best sorting solution depends on many factors, including the amount of data being sorted, the type of data being sorted, and any time and space constraints. It is useful to familiarize yourself with the other sorting algorithms that are available before determining which is best for your application.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.