简体   繁体   English

排序/比较不同的标准

[英]Sorting/Comparing with different criterions

So I have data coming in from STDIN and I'm creating objects per line that holds a numeric data field. 所以我有来自STDIN的数据,我每行创建一个包含数字数据字段的对象。 Based on this field, I want to sort them into either a PriorityQueue or LinkedList. 基于此字段,我想将它们排序为PriorityQueue或LinkedList。 After this sorting, I am going to calculate another value for the objects to hold (the values will be based on frequencies and such). 在这个排序之后,我将计算要保持的对象的另一个值(值将基于频率等)。 I then want to sort this into another PQ or LL using the second value as the new criterion. 然后,我想使用第二个值作为新标准将其排序为另一个PQ或LL。

What's the best way to go about sorting the same objects twice using different criterions each time? 每次使用不同的标准对同一对象进行两次排序的最佳方法是什么? I don't want to have to create two types of the same object with just distinct compareTo methods. 我不想只使用不同的compareTo方法创建两种类型的同一对象。 I'm hoping theres a better way. 我希望有更好的方法。

Insead of implemnting Comparable in the objects themsevles , you should use compartors: http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html 在对象中实现Comparable的内容,你应该使用隔离专区: http ://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

This enables you to compare the same objects with different compare logic. 这使您可以使用不同的比较逻辑比较相同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM