Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Sorting in way that A comes before a and B comes before b

I have a List of colors like this:

Pink, Blue, Red, blue, Grey, green, purple, black ...etc

List<String> listOfColors =  Arrays.asList("Pink", "Blue", "Red", "blue", "Grey", "green", "purple", "black");

There are some intermediate operation like filtering some fruit colors, now I am left with filtered results where I want them to be sorted in order:

Blue, black, blue, Grey, green, Pink, purple, Red

I have tried :

List<String> collect = listOfColors.stream().sorted(String::compareToIgnoreCase)
        .collect(Collectors.toList());

It does not work as expected.

The output is the following:

black, Blue, blue, green, Grey, Pink, purple, Red

I want the following:

Blue, black, blue, Grey, green, Pink, purple, Red

like image 637
Vishwa Ratna Avatar asked Jan 31 '20 06:01

Vishwa Ratna


People also ask

What comes before B in a list?

If returned value < 0, a is sorted before b ( a comes before b ). If returned value > 0, b is sorted before a ( b comes before a ). If returned value == 0, a and b remain unchanged relative to each other. If a.length - b.length < 0, a comes before b. For example, "Adam" comes before "Jeffrey" as 4 - 7 < 0.

How do I create a custom sort order in Excel?

Create a custom sort order that is not governed by the data and does not require a calculation. Open a visualization. Click the Field icon. From the column that you want to custom sort click the menu icon. You can sort the data in either the x-axis or y-axis, depending on what type of data is in an axis.

How will the elements be sorted when a function returns 0?

How the elements will be sorted depends on the function’s return value: if it returns a negative value, the value in a will be ordered before b. if it returns 0, the ordering of a and b won’t change. if it returns a positive value, the value in b will be ordered before a.

How do you sort an array alphabetically rather than alphabetically?

Suppose we want to sort the above names array such that the longest name comes last, rather than sorting it alphabetically. We can do it in the following way: Here, the sorting is based on the logic a.length - b.length. It basically means that the item with shorter length will appear at the beginning of the Array.


1 Answers

You can use RuleBasedCollator to define your own Rules.

Example of custom rule:

String rules = "< c,C < b,B";

The above rule is decoded as that both uppercase and lowercase C's are to appear before both uppercase and lowercase B's when comparing strings.

String customRules = "<A<a<B<b<C<c<D<d<E<e<F<f<G<g<H<h<I<i<J<j<K<k<L<l<M<m<N<n<O<o<P<p<Q<q<R<r<S<s<T<t<U<u<V<v<X<x<Y<y<Z<z";
RuleBasedCollator myRuleBasedCollator = new RuleBasedCollator(customRules);
Collections.sort(listOfColors,myRuleBasedCollator);
System.out.println(listOfColors);

Output:

[Blue, black, blue, Grey, green, Pink, purple, Red]

Edit: Instead of writing the customRules by hand, you can use the below to code to generate it.

String a = IntStream.range('a', 'z' + 1).mapToObj(c -> Character.toString((char) c))
        .flatMap(ch -> Stream
            .of("<", ch.toUpperCase(), "<", ch)).collect(Collectors.joining(""));
like image 80
Vishwa Ratna Avatar answered Oct 04 '22 04:10

Vishwa Ratna