sorting – How to sort and extract a list containing products

First, you must write products with a space, otherwise it is considered a name. Note that A A will be written as A square.

I assume that elements of different sectors or not already ordered alphabetically as in your example (Sector1: A,B, Sector 2: K,R). In this case sorting would be trivial.

To sort lists according to elements of sector 2 we first extract these elements. Then we use Order to determine if the products are in order or not.

Now you can sort ascending or descending. For the first case you would have:

list = {A K R, A K B, A B A};
Sort(list, Order(Cases(#1, K | R), Cases(#2, K | R)) &)
(*{A^2 B, A B K, A K R}*)

For descending sort you would have to change the sign of Order:

list = {A B A, A K R, A K B};
Sort(list, -Order(Cases(#1, K | R), Cases(#2, K | R)) &)
(*{A K R, A B K, A^2 B}*)