Search code examples
javaalgorithmsortingcomparisoncomparable

Sort algorithm problems on java comparable


I want to do a specific sort. I am using java's comparable interface which means the return of my compare method must return -1 +1 or 0 depending on the equality of the two compared, then I am sorting using Collections. My trouble comes from how I wish to compare.

I have a key that is made up of either of the following

[keyName]  
[siteName].[keyName]  
[siteName].[pageName].[keyName]  

so as an example "mysite.alampshade.color"

the tricky part is the sites must be sorted first, followed by keyname, followed by pageName. but firstly by the keynames, then site name, in the order of the number of sections to the property. Sorry. its a little complicated, an example may help. here is the order they must be:

alpha  
beta  
charlie  
sitea.alpha  
sitea.charlie  
sitea.pagea.beta  
sitea.pageb.beta  
sitea.pagea.charlie  
siteb.alpha  
siteb.delta  
siteb.pagef.alpha  
siteb.pageb.echo  
siteb.pageb.golf  
siteb.pagea.hotel  
siteb.pageb.hotel  
siteb.pagec.hotel  

I have tried many different ways and have thrown away code a few times but still cant get it perfect. some pseudocode would be of great help if not some java.

EDIT: to add another possibly simplier to understand example the following is sorted how I need it

a  
b  
c  
z  
a.b  
a.c  
a.d  
a.z  
a.b.a  
a.c.a  
a.b.b  
a.b.c  
a.c.c  
a.a.d  
b.a  
b.b  
b.z  
b.a.a  
b.b.a  
b.a.b  
c.c.f  

Solution

  • Another option, making it recursive you avoid the problem if there is ever more entries.

    import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List;

    public class SortTest {
        public static void main(String[] args) {
            String[] test = new String[]{
                "a",
                "b",
                "b.a",
                "b.a.a",
                "a.a.a",
                "a.b.a",
                "a.a",
                "a.b",
                "b.a.b",
                "b.b.a"
            };
    
            Arrays.sort(test, new Comparator<String>() {
    
            int compareComplexList(List<String> a, List<String> b, List<int[]> positions, int order ) {
    
              int minimum = a.size() < b.size() ? a.size() - 1 : b.size() - 1;   
    
              if (a.get(positions.get(minimum)[order]).compareTo(b.get(positions.get(minimum)[order])) != 0)
                    return a.get(positions.get(minimum)[order]).compareTo(b.get(positions.get(minimum)[order]));
              else if (order < minimum - 1) return compareComplexList(a,b, positions, ++order);
              else return Double.compare(a.size(),b.size());
            }
    
            public int compare(String a, String b) {
              List<String> partsA = Arrays.asList(a.split("\\."));
              List<String> partsB = Arrays.asList(b.split("\\."));
              List<int[]>  orders = new ArrayList<int[]>();
    
              orders.add(new int[] {0});
              orders.add(new int[] {0,1});
              orders.add(new int[] {0,2,1});
    
              return compareComplexList(partsA, partsB, orders,0);
            }
            });
            System.out.println("Sorted: "+Arrays.toString(test));
        }
    
    }