Reverse view of trees while maintaining sorting at each level

I am just exploring Collections and I have a task. Some organizations want to create department directories. Department codes are an array of strings:

    "K1\SK1"
    "K1\SK2"
    "K1\SK1\SSK1"
    "K1\SK1\SSK2"
    "K2"
    "K2\SK1\SSK1"
    "K2\SK1\SSK2" 

      

I need to sort the tick codes and grow and maintain the hierarchy. If necessary, add a String with the code of higher-level departments, for example, here we have lines with K1, but we do not have a separate line "K1". After sorting, the result should be

ascending sort:
        "K1"
        "K1\SK1"
        "K1\SK1\SSK1"
        "K1\SK1\SSK2"
        "K1\SK2"
        "K2"
        "K2\SK1"
        "K2\SK1\SSK1"
        "K2\SK1\SSK2"

descending sort:
        "K2"
        "K2\SK1"
        "K2\SK1\SSK2"
        "K2\SK2\SSK1"
        "K1"
        "K1\SK2"
        "K1\SK1"
        "K1\SK1\SSK2"
        "K1\SK1\SSK1"

      

And the question is how to sort the departments descending while maintaining the hierarchy? When im adding Strings to TreeSet it is ok, natural sort works and sorts the sodes in ascending order. But when I try to sort downstream with a comparator, it sorts without preserving the hierarchy as expected. The way I think I need to traverse the tree from right to left on the parent nodes. But how to do that? Here is my code:

    public class SortDepartment {

/**
 * Adds departments and sorts it in natural sorting in set
 * @param departments
 * @return
 */
public Set<String> addDepartmentIfNecessaryAndSortAscending(List<String> departments){

    Set<String> result = new TreeSet<>();
    String temp;
    for(int i = 0; i < departments.size(); i++) {

        if(departments.get(i).contains("\\")) {
            temp = departments.get(i).substring(0, departments.get(i).lastIndexOf("\\"));
            result.add(temp);
        }
        result.add(departments.get(i));
    }
    return result;
}


/**
 * Sorts departments descending
 * @param departments
 */
public Set<String> sortDepartmentDescending(Set<String> departments){

    Set<String> result =  new TreeSet<>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o2.compareTo(o1);
        }
    });

    result.addAll(departments);
    return result;
}

      

}

+3


source to share


1 answer


One way to do this is to create a department class. This department class will have two fields:

String name; int hierarchy;

Now you can create two comparators:

Comparator<Department> sortDescending = (d1, d2) -> {
    if(d1.hierarchy != d2.hierarchy)
        return Integer.compare(d2,d1)
    return String.compare(d2.name, d1.name)
}

      



and

Comparator<Department> sortAscending = (d1, d2) -> {
    if(d1.hierarchy != d2.hierarchy)
        return Integer.compare(d2,d1)
    return String.compare(d1.name, d2.name)
}

      

This can only work if you have a way to know the relative hierarchy of each department before the start of each object.

+1


source







All Articles