# How best to organize sorting by multiple parameters?

Good time of day.

The essence of the goal:
There are two variables, say cat and dog, each of which stores a single float value, and the third variable text with some text specific to these two variables.
``````cat = 30.0
dog = 1.0
text = 'description'``````

Each time through the loop the values in all three variables change.
They need to be stored in some type of dictionary (cat:dog) and then sort by cat and dog at the same time.
The principle of the following sort - cat at the same time should be starting with the smaller dog and from the larger and display the result, which includes also the most variable text linked to these two values.

Had the idea to make the same dictionary but I have no idea how to sort of maintaining the relationship cat and dog to a variable text (and indeed how to tie it text).

Example of desired result (which does not include text, as I don't know how to attach it here):
``````dic = {}
cat = 993.0
dog = 4.0
dic.update({cat:dog})
> {993.0: 4.0}
cat = 467.0
dog = 915.0
dic.update({cat:dog})
> {993.0: 4.0, 467.0:915.0}
.... # and again n
> {993.0: 4.0, 467.0:915.0, 81.0:457.0, 8148.0:119.0, 550.0:43.0, 130.0:91.0}
# then sorted and you want to get a cat ascending, descending dog:
> {81.0:457.0, 130.0:91.0, 467.0:915.0, 550.0:43.0, 993.0:4.0, 8148.0:119.0}``````

Question: How to get a sorted dictionary (and maybe something better than a dictionary) for cat and dog (as in example above) + text?

Would be eternally grateful for any advice.
July 2nd 19 at 18:17
July 2nd 19 at 18:19
Solution
Very trivial You doing with the sorting. The word dictionary sorting is not supported, so I would abandon it (especially considering the fact that You have not a pair of variables, and three).

In order to arrange access to Your three variables, no classes no need to write enough to use a namedtuple:
``````>>> import collections
>>> pair = collections.namedtuple('pair',['cat','dog','text'])``````

To keep "couples" better to the list:
``````>>> vlist=[]
>>> vlist.append(pair(900,2,'foo'))
>>> vlist.append(pair(500,4,'bar'))
>>> vlist.append(pair(300, 10, 'foobar'))
>>> vlist
[pair(cat=900, dog=2, text='foo'), pair(cat=500, dog=4, text='bar'), pair(cat=300, dog=10, text='foobar')]``````

This will be supported by sorting by the first argument (cat):
``````>>> vlist.sort()
>>> vlist
[pair(cat=300, dog=10, text='foobar'), pair(cat=500, dog=4, text='bar'), pair(cat=900, dog=2, text='foo')]``````

Note that we will have a modified list that will allow You to sort it cycle as You want (for example, if the value cat from the neighboring elements are equal, sort in reverse order by the value of the dog (do I understand correctly that You wanted it?)).
July 2nd 19 at 18:21
The principle of the following sort - cat at the same time should be starting with the smaller dog and from the greatest

In the General case it's impossible. Here's how your sort {1, 1} and {2, 2}?

Well, the dictionary you most likely will not work, because re-adding the key does not generate a new pair, and updates the existing.

Create a class with 3 fields, implement it for the comparison operator, which specify the required order relation (as already I mentioned above, what you propose is impossible to implement), put the objects in a list and sort, the standard sort.
July 2nd 19 at 18:23
To separate text from numbers is not a good idea. Unless you store them in a separate dictionary
``````def compare(x, y):
if x[0] < y[0]:
return -1
elif x[0] > y[0]:
return 1
else:
if x[1] > y[1]: #the signs are inverted, it is necessary to sort by the 2nd variable in reverse order
return -1
elif x[1] < y[1]:
return 1
else:
return 0

subjects = {(30.0,1.0,'description'), ...}

sortedDict = sorted(subjects, cmp=compare)``````

Well still it is necessary to consider that a floating-point number is compared with the use of eps. Type x < y+eps, x > y-eps

Find more questions by tags Python