Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list alphabetically with a specific value at head (if present)

I'm looking for the most readable definition for a comparator that satisfies the following test case:

@Test
public void testComparator() {
    List<String> toSort = Lists.newArrayList("a", "b", "c", "d", "e", "f");
    Collections.shuffle(toSort);
    Comparator<String> theSolution = ???; 
    Collections.sort(toSort, theSolution);
    System.out.println(toSort); // Prints [c, a, b, d, e, f]
}

I've tried a comparator using Guava's Ordering defined like this:

Ordering.explicit("c").thenComparing(Ordering.natural());

However, explicit throws an exception for items not enumerated. So that solution fails. Any suggestions?

like image 475
Spina Avatar asked Aug 23 '16 03:08

Spina


People also ask

How to sort list alphabetically using jQuery?

Given a list of elements, The task is to sort them alphabetically and put each element in the list with the help of jQuery. jQuery text () Method: This method set/return the text content of the selected elements. If this method is used to return content, it provides the text content of all matched elements (HTML tags will be removed).

How to sort words in alphabetical order in C #?

This guide shows how to sort words in alphabetical order in c#. There is a built-in function in c# that we can use to sort a list. First of all, using System.Collections.Generic;, this is the library you need to import to use the list in c#.

How do I sort a list alphabetically in Python?

Here’s the code for a simple sort: Above, the list mylist holds the strings “world”, “LearnPython.com”, “pineapple”, and “bicycle” in no particular order. We want to sort this list and store it as a new variable called sorted_list. In Python, sorting a list alphabetically is as easy as passing a list of strings to the sorted () method.

How to sort a list of elements in Java?

Example 1: In this example, first the list elements are selected and then passed to a function for sorting. After sorting they are appended to the Parent element using appendTo () method in sorted manner. Example 2: In this example, First the list elements are selected and then passed to a function for sorting.


2 Answers

You could explicitly write a comparison function, e.g.

Comparator<String> theSolution = Comparator.comparing(a -> a.equals("c") ? "" : a);
// treat "c" as the same as the empty string "" when sorting which will be ranked first.
like image 133
kennytm Avatar answered Oct 16 '22 22:10

kennytm


It's unclear from your example if you want the members that are not "c" to remain in their current order or be sorted into alphabetical order. If you want to sort "c" to the front and leave the remainder then you could use:

Comparator.comparing("c"::equals).reversed();

Reversing the comparator is required because the natural ordering for a boolean is false first.

Sorting the remaining items in alphabetical order can be achieved by:

Comparator.comparing("c"::equals).reversed()
    .thenComparing(Object::toString, String::compareTo);
like image 3
sprinter Avatar answered Oct 16 '22 22:10

sprinter