Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a comparator function to sort

So I'm working with a few pre-existing comparators that compare certain values in two tuples and return true if the first is greater than the second, false if otherwise. Here's the code for one of them:

def cmpValue(subInfo1, subInfo2):     """     Returns True if value in (value, work) tuple subInfo1 is GREATER than     value in (value, work) tuple in subInfo2     """     # TODO...     if subInfo1[0] > subInfo2[0]:         return True     else:         return False 

Now, I have a dictionary that has numerous tuple entries of the type being compared above. I want to sort them all in reverse order, but I don't really understand how I would accomplish that. I was thinking something like:

sortedDict = sorted(subjects, key=comparator, reverse = True) 

But I don't know what to pass into the comparator because each comparator takes two arguments (subInfo1, subInfo2). I cannot change the comparator functions.

like image 255
user1427661 Avatar asked Oct 05 '12 15:10

user1427661


People also ask

How does comparator function works in sorting?

If you compare age first and then compare name if the ages are equal, the sequence will be sorted by age -- but within each subsequence where the age is equal, that subsequence is sorted by name.

How do I sort using comparator?

Method 2: Using comparator interface- Comparator interface is used to order the objects of a user-defined class. This interface is present in java. util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using a comparator, we can sort the elements based on data members.

How does comparator function works in C++?

The comparator class compares the student to be searched from the list of students on the basis of their name attribute. If the name attribute of the object to be searched is equal to any of the object's name attribute in the list then it returns true, otherwise, it returns false.

How do you sort an object using comparator and comparable?

In Java, we can implement whatever sorting algorithm we want with any type. Using the Comparable interface and compareTo() method, we can sort using alphabetical order, String length, reverse alphabetical order, or numbers. The Comparator interface allows us to do the same but in a more flexible way.


1 Answers

You're passing the comparator as the key function. You should be passing it as the cmp, wrapped in some kind of function that turns it into a proper comparator.

def make_comparator(less_than):     def compare(x, y):         if less_than(x, y):             return -1         elif less_than(y, x):             return 1         else:             return 0     return compare  sortedDict = sorted(subjects, cmp=make_comparator(cmpValue), reverse=True) 

(Although actually, you should be using key functions:

sorted(subjects, operator.itemgetter(0), reverse=True) 

Also note that sortedDict will not actually be a dict, so the name is rather confusing.)

like image 95
Fred Foo Avatar answered Sep 23 '22 00:09

Fred Foo