sort a List based on a parameter available in another class

后端 未结 2 1636
名媛妹妹
名媛妹妹 2021-01-23 08:29

I have some design problems with Java Comparator Interface.

I have a class which contains a Set of a simple custom data structure:



        
相关标签:
2条回答
  • 2021-01-23 08:48

    You could use something that smells like higher order functions. That is, make a static function that takes a map of sorts from Long to int (which is the priority) or data and returns a new Comparator.

    The class Foo has a static method getComparator which takes an Orange. An Orange is a class that has a method getPriority which takes an ID an return the corresponding priority. The getComparator method constructs a new Comparator object. The new Comparator object's compare method takes two IDs. It looks up the corresponding priorities of the two IDs and compares them.

    public interface Orange {
        // Looks up id and returns the corresponding Priority.
        public int getPriority(Long id);
    }
    
    public class Foo {
        public static Comparator<Long> getComparator(final Orange orange) {
            return new Comparator<Long>() {
                public int compare(Long id1, Long id2) {
                    // Get priority through orange, or
                    // Make orange juice from our orange.
                    // You may want to compare them in a different way.
                    return orange.getPriority(id1) - orange.getPriority(id2);
            };
        }
    }
    

    My java is a bit rusty so the code may be flawed. The general idea should work, though.

    Usage:

    // This is defined somewhere. It could be a local variable or an instance
    // field or whatever. There's no exception (except is has to be in scope).
    Collection c = ...;
    ...
    Orange orange = new Orange() {
        public int getPriority(Long id) {
            // Insert code that searches c.mySet for an instance of data
            // with the desired ID and return its Priority
        }
    };
    Collections.sort(c.myList, Foo.getComparator(orange));
    

    I have not given an example on how an Orange could look.

    0 讨论(0)
  • 2021-01-23 09:00

    I assume that you have a List<Data> stored somewhere.. In Comparator, you need to invoke a getDataById method from your data class, and sort through Priority..

    Check the below code.. I have used a single class for many purpose..

    Ideally you would want to break it into more classes.. But this is just a Demo, how to achieve what you want..

    class Container {
        // List of Data  instances created..
        // This list has to be static, as it is for a class, 
        // and not `instance specific`
        public static List<Data> dataList = new ArrayList<Data>();
    
        // List of Ids, that you want to sort.
        private List<Long> idList = new ArrayList<Long>();
    
        // Populate both the list..
    
        // Have a method that will iterate through static list to 
        // find Data instance for a particular id
    
        public static Data getDataById(long id) {
            // Find Data with id from the list
    
            // Return Data
        }
    
        public void sortList() {
            Collections.sort(idList, new MyComparator());
        }
    }
    
    public MyComparator implements Comparator<Long> {
    
        public int compare(Long int1, Long int2) {
            Data data1 = Container.getDataById(int1);
            Data data2 = Container.getDataById(int2);
    
            return data1.getPriority() - data2.getPriority();
        }
    }
    
    0 讨论(0)
提交回复
热议问题