How to sort two different objects in a collection?

后端 未结 4 1970
夕颜
夕颜 2021-01-21 16:46

Suppose I have two classes CLassA and CLassB. And they have one atributte in common, for example the number of elements that each class holds.

How can i create a collect

4条回答
  •  悲&欢浪女
    2021-01-21 17:39

    Really ClassA and ClassB should be related either through an inheritance hierarchy, or by a common interface if you are going to put them both in the same collection.

    The simplest thing would be to have a common interface that provides an accessor method for the common attribute. And then the comparator could use that method (through the interface) for fetching the value from ClassA's instance as well as ClassB's instance.

提交回复
热议问题