comparator

Scala: Is there a way to use PriorityQueue like I would in Java?

I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but I don't want to make it Ordered[A] just for this one purpose. I don't consider the ordering I want to use with respect to the PriorityQueue as the natural ordering of the class. class MyObject (sequence: Int, values: List[String]) ... So, in my ...

Java: fillings two vectors and sorting them

How can I fill up two vectors and combine them with eachother (either index or an object type in class). I guess if I use index to combine them, then the sorting can be handeld with Collections.sort otherwise I have to create a comparator. On plus we have to code down to the 1.4 convention. No generics pls... To make your imagination e...

Is it OK to have a Java Comparator where order can change dynamically?

I have a set of time stamped values I'd like to place in a sorted set. public class TimedValue { public Date time; public double value; public TimedValue(Date time, double value) { this.time = time; this.value = value; } } The business logic for sorting this set says that values must be ordered in desc...

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

Hi all! The title is the main question. The exact scenario (I am 'using namespace std;'): void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) { list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator); } This is the comparator definition: class Substring { // ... class...

writing a Comparator for a compound object for binary searching

I have a class, and list of instances, that looks something like this (field names changed to protect the innocent/proprietary): public class Bloat { public long timeInMilliseconds; public long spaceInBytes; public long costInPennies; } public class BloatProducer { final private List<Bloat> bloatList = new ArrayList<Blo...

Junit inline comparator initialization error

Hi, I've created a SortedList Class, which has a Constructor that takes a java.util.Comparator as an argument. After running Unit tests on my machine (through eclipse 3.3.0), everything was OK. However, Hudson complaints because it says it can't instantiate my comparator. Here its my simple test (snippets) public class SortedListTest...

Nice general way to sort nulls to the bottom, regardless?

I'm writing some custom Comparators, and I'd like them to push null items to the bottom of the list, regardless of whether I'm sorting ascending or descending. What's a good strategy or pattern for approaching this? Offhand: simply write separate ascending and descending comparators, sharing code where possible; delegate null handli...

Natural sort order string comparison in Java - is one built in?

I'd like some kind of string comparison function that preserves natural sort order1. Is there anything like this built into Java? I can't find anything in the String class, and the Comparator class only knows of two implementations. I can roll my own (it's not a very hard problem), but I'd rather not re-invent the wheel if I don't hav...

Java: SortedMap, TreeMap, Comparable? How to use?

I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will i...

Java: What is the difference between implementing Comparable and Comparator?

I have seen both used. When would you use one over the other? ...

How does Javascript's sort() work?

How does the following code sort this array to be in numerical order? var array=[25, 8, 7, 41] array.sort(function(a,b) { return a - b}) I know that if the result of the computation is... Less than 0: "a" is sorted to be a lower index than "b". Zero: "a" and "b" are considered equal, and no sorting is performed. Greater th...

Java SortedSet + Comparator, consistency with equals() question

I'd like to have a SortedSet of Collections (Sets themselves, in this case, but not necessarily in general), that sorts by Collection size. This seems to violate the proscription to have the Comparator be consistent with equals() - i.e., two collections may be unequal (by having different elements), but compare to the same value (becaus...

Crafting a Comparator object for sorting generic List<? extends T> using Collections.sort()

I am trying to implement a generic sort utility method for a List of objects of any class that implements MyInterface. Per Java API (http://java.sun.com/javase/6/docs/api/java/util/Collections.html), Collections.sort() method signature is: public static <T> void sort(List<T> list, Comparator<? super T> c) I am not sure if List with a ...

STL Priority Queue on custom class

I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator<(Node &aNode); ... } Node.cpp #include "...

Java: Trouble with TreeSet and LinkedList

I have an unsorted linked list. To sort it, I thought I'd put the values into a TreeSet with a comparator supplied, then return those values as a new linked list. Yet, it fails. Comparator: public class SortSpeciesByCommonName implements Comparator<Species> { /** * a negative integer, zero, or a positive integer as the first ...

Help needed to write a comparator for my job interview code sample

I need help to write a comparator :- I want this output :- Martin Joseph Male 4/2/1979 Green Ramya Patil Female 5/4/2009 Red Don kelly Male 5/6/1986 Yellow Van Shinde female 3/4/1984 Green But i am getting the following output :- Output 1: Van Shinde female 3/4/1984 Green Don kelly Male 5/6/1986 Yellow Ram...

How can I improve this Comparator?

I've got a few Comparators -- one for Dates, one for decimals, one for percentages, etc. At first my decimal comparator looked like this: class NumericComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { final Double i1 = Double.parseDouble(s1); final Double i2 = Double.parseDoubl...

Java, how to add a "comparator" class to increase code reusability

Preface: I'm posting this on behalf of a friend (who's apparently to shy to post it himself), I browsed through the related questions and I didn't seem to find any duplicate.. But do note that I don't know Java at all so I apologize in advance if this is a duplicate! This is part of the code: public class ElencoEsami implements Compara...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String description; Letter() { description = toString(); } Letter(String description) { this.description = description; ...

Creating an universal comparator to sort a tree map problem.

This is where I'm at: public final Comparator<Object> ID_IGN_CASE_COMP = new Comparator<Object>() { public int compare(Object o1, Object o2) { String s1 = null; String s2 = null; try { Class c = o1.getClass(); IO.println(c.getName()); //java.lang.string instead of Animal M...