0

I am in the process of writing some code to generate graphs/metrics for climbing logs my friends and family have kept over the years. One problem I am running into relates to sorting of Yosemite climbing grades.

Running the following code:

   testlist = ["5.10a", "5.5", "5.8+", "5.11b", "5.6"]
   sortedtestlist = sorted(testlist)
   print(sortedtestlist)

Results in:

['5.10a', '5.11b', '5.5', '5.6', '5.8+']

5.5 should occur before 5.10a. It appears that sorted is only looking at a depth of one number? I stripped the "5." out of the grades, sorted, and re-added them later and I got the same behavior.

The results I am trying to get are:

['5.5', '5.6', '5.8+', '5.10a', '5.11b']

I know if it comes down to it, I can probably write my own sorting function and past that to sorted. Is there an easier way?

ShaneK
  • 193
  • 9
  • 1
    This has nothing to do with precision. You are sorting strings, not numbers – DeepSpace Mar 11 '21 at 18:37
  • You are sorting *strings*. Strings are ordered lexicographically (if we considered only alphabetic characters, this would be equivalent to alphabetically, like how words are ordered in a dictionary) – juanpa.arrivillaga Mar 11 '21 at 18:38

0 Answers0