0

Input:

{'Thiem': (3, 0, 10, 104, 11, 106), 
 'Medvedev': (1, 2, 11, 106, 10, 104), 
 'Barty': (0, 2, 8, 74, 9, 76), 
 'Osaka': (0, 4, 9, 76, 8, 74)}

The expected output should be sorted based on Values of Dict, in the order of attributes in values tuple. Like, firstly on 1st field value(desc), if matching then on 2nd value(desc), till 4th field(desc) and Ascending on 5th & 6th field. I tried using sorted() method in a couple of ways.

output:

Thiem 3 0 10 104 11 106
Medvedev 1 2 11 106 10 104
Osaka 0 4 9 76 8 74
Barty 0 2 8 74 9 76

Kindly assist or suggest an approach.

Edit: Updated description for more clarity. Below is the code i tried:

>>> results=[]
>>> for (k,v) in d.items():
    results.append(v)
>>> results.sort(key= lambda x: (x[4],x[5]))
>>> results.sort(key= lambda x: (x[0],x[1],x[2],x[3]), reverse=True)
VimalK
  • 65
  • 1
  • 8
  • 1
    Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") Please be aware this is not a code-writing or tutoring service. We can help solve specific, technical problems, not open-ended requests for code or advice. Please edit your question to show what you have tried so far, and what specific problem you need help with. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Feb 21 '21 at 20:22
  • 1
    "in the order of attributes in values tuple. Like, firstly on 1st column value(desc), if matching then on 2nd value." that is how `tuple` objects sort already, lexicographically. What did you try *exactly*? – juanpa.arrivillaga Feb 21 '21 at 20:24
  • Your expected output is really not sorted. The keys are not sorted either in ascending or descending, your values are also not sorted in ascending or descending. So what is your actual expected output ? – Joe Ferndz Feb 21 '21 at 20:24
  • @JoeFerndz it's sorted in reverse order based on the values – juanpa.arrivillaga Feb 21 '21 at 20:26
  • @juanpa.arrivillaga. you mean column wise sorting in reverse order? Thats the only logical way i can see sorting happening – Joe Ferndz Feb 21 '21 at 20:31
  • @JoeFerndz I'm not sure what you mean by "columnwise", this is basically `sorted(data.items(), key=lambda x: x[1], reverse=True)` – juanpa.arrivillaga Feb 21 '21 at 20:32
  • @VimalK, if you want to use pandas, this will be a simple solution. – Joe Ferndz Feb 21 '21 at 20:32
  • @JoeFerndz pandas is no more convenient than just using the built-in `sorted` – juanpa.arrivillaga Feb 21 '21 at 20:33
  • @juanpa.arrivillaga, sorry about that. I see it now. It is just a simple sort – Joe Ferndz Feb 21 '21 at 20:35
  • Do these answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) and [Sort list of lists ascending and then descending](https://stackoverflow.com/q/6666748/4518341) – wjandrea Feb 21 '21 at 22:36
  • @VimalK I believe people didn't quite understand your objective, but I believe I do – Omar AlSuwaidi Feb 21 '21 at 22:40
  • @wjandrea Thanks for your input. Yes, this link points in right direction whrein given dict has only singleton value per key. For my requirement, value attribute of dict has a tuple of 6 fields per key() and i need to perform custom sort using composite key ( 3 columns in Asc followed by 2 column desc ). each value can be thought as row in excel and doing a custom sort. – VimalK Feb 22 '21 at 06:47
  • @VimalK Yes, the second link I mentioned covers that, though it's a simpler structure. LMK if you don't see the resemblance, and I'll write you an answer. – wjandrea Feb 22 '21 at 23:51

1 Answers1

0

I believe you are trying to compare the first number (element) of every tuple with one another, and the key with the greatest number should go on top (0 index). In case the numbers were equal; you would instead compare the second number of every tuple, and so on... till you reach the final element in that tuple. If so, then:

def dic_sorted(dic):
    for r in range(len(dic) - 1):
        i = 0
        values = list(dic.values())
        while values[r][i] == values[r + 1][i]:
            i += 1
        if values[r][i] < values[r + 1][i]:
            key_shift(dic, values[r])

    return dic


def key_shift(dic, v1):
    keys = list(dic.keys())
    values = list(dic.values())
    temp_key = keys[values.index(v1)]
    del dic[temp_key]
    dic[temp_key] = v1


for i in range(5):  # Number of iterations depends on the complexity of your dictionary
    dic_sorted(data)
print(data)
Omar AlSuwaidi
  • 1,187
  • 2
  • 6
  • 26
  • Yes, Thanks. you got my requirement right. I will try to implement a bit more of customization like descending order for first n values and then ascending for n column values. With above, it is implemented for all columns either Asc or Desc. To put it simply, i want to implemnt similar to "custom sort" feature of excel on each values tuple. – VimalK Feb 22 '21 at 06:56