Sorting string list in ascending and descending order in Java

Sorting string list in ascending order in Java is straight forward.

Below code will sort string list in ascending order,

	public static void main(String[] args){

		List<String> stringList = new ArrayList<>();
		stringList.add("Suzuki");
		stringList.add("Honda");
		stringList.add("Toyota");
		stringList.add("Ford");
		stringList.add("Renault");
		stringList.add("Hyundai");

		// before sorting
		System.out.println(stringList);

		// sort the list
		Collections.sort(stringList);

		// after sorting
		System.out.println(stringList);
	}
// before sorting
[Suzuki, Honda, Toyota, Ford, Renault, Hyundai]

// after sorting
[Ford, Honda, Hyundai, Renault, Suzuki, Toyota]

Here I’m adding car company names to string list and passing it to Collections sort method. The sort method will sort string list items in ascending order. Notice that sort method will return void, instead it will manipulate the original list object that we passed to it so when we print the list object, string items are printed in ascending order.

But sorting string list in descending order is not straight forward as ascending order. To understand sorting in descending order first we need to understand how sorting mechanism work. To understand sorting mechanism please go through my another post Understanding string list sorting in Java.

To sort string list in descending order we have to use overloaded sort method of Collections class.

public static <T> void sort(List<T> list, Comparator<? super T> c)

It takes two parameters, list to be sorted and comparator object. Comparator object is the object where our sorting logic resides. First we need to write our own Comparator class where our sorting logic resides for string sorting and then pass its object to sort method

public class MyComparator implements Comparator<String>{
		@Override
		public int compare(String o1, String o2) {
			return o2.compareTo(o1); // magic line
		}
	}

The highlighted line in the above code is where all the magic happens. This line is responsible for sorting list in ascending and descending order. o2.compareTo(o1) will make sort method to sort list in descending order but If we change it to o1.compareTo(o2) then list will be sorted in ascending order.

Now pass MyComparator object to sort method along with string list to be sorted and run the code.

	public static void main(String[] args){

		List<String> stringList = new ArrayList<>();
		stringList.add("Suzuki");
		stringList.add("Honda");
		stringList.add("Toyota");
		stringList.add("Ford");
		stringList.add("Renault");
		stringList.add("Hyundai");

		// before sorting
		System.out.println(stringList);

		// sort the list
		MyComparator myComparator = new MyComparator();
		Collections.sort(stringList, myComparator);

		// after sorting
		System.out.println(stringList);
	}
// before sorting
[Suzuki, Honda, Toyota, Ford, Renault, Hyundai]

// after sorting
[Toyota, Suzuki, Renault, Hyundai, Honda, Ford]

Notice that string list items are printed in descending order.

Here is the helper method you can use for sorting string list in ascending and descending order,

	public void listSortEx(List<String> list, final Boolean descOrder){
		Collections.sort(list, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				if(descOrder){
					return o2.compareTo(o1);
				}else{
					return o1.compareTo(o2);
				}
			}
		});
	}

Using this method is easy. Just pass it a string list to be sorted and a boolean value true if you want to sort list in descending order or false to sort list in ascending order. Notice that this function will return void, so the original list object you pass will be manipulated instead.

3 thoughts on “Sorting string list in ascending and descending order in Java

  1. This line is responsible for sorting list in ascending and descending order. o2.compareTo(o1) will make sort method to sort list in descending order but If we change it to o1.compareTo(o2) then list will be sorted in ascending order.
    How is this determined? Why placing o2 first sorts the list in descending order and vice versa

Leave a comment