# Sort a list

#1

Hey everyone,

I tried to build a function that actually sort a list of tuple, the functions first sort according the second element, and if the second element is the same for some tuple, is also sorts the first element.
for example, giving [(1,10),(7,4),(5,4)], it should return [(1,10),(5,4),(7,4)]

The problem, is, I don’t see where my mistake is, could someone help ?

#2

Before looking for a mistake, what makes you say there is one?
And, why are you implementing your own sort?

I suggest using built-in sort or if you specifically intend to write your own sort, then accept a comparison function or key function instead of making a sort that only works for that special case

Also, this is worse than bubble sort which is already bad. Mergesort and Quicksort are much better and they’re both simple:

Mergesort:
sort the left half and the right half by calling self
then merge both results by taking the smallest value out of the two results until everything has been included

Quicksort:
choose a value (random?), split into two lists where one is all smaller than that value, and the other contains the rest. sort both recursively and then concatenate the results

Note that for both of those sorts a special case is needed for an empty list where the result is an empty list. (Otherwise they’d keep recursing forever)

(but python’s built in sort is still a bit better)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.