Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Sorting
#1
I am trying to understand how to sort an array in Python 3.x using the bulit in sort function. So I wrote the following code. However, it did does not work.

    a = [1,-2,3,-4,5,-6]
    sorted(a, cmp = numeric_compare ) 
where numeric_compare is defined as follows:
def numeric_compare(x, y):
    return abs(x) - abs(y)
What I want to do is sort the array by the absolute values of the numbers. I realize, in this case the array is already sorted in that order.

Bob
Reply


Messages In This Thread
Help with Sorting - by rsherry8 - Jan-28-2018, 03:36 PM
RE: Help with Sorting - by buran - Jan-28-2018, 04:10 PM
RE: Help with Sorting - by rsherry8 - Jan-28-2018, 04:44 PM
RE: Help with Sorting - by j.crater - Jan-28-2018, 04:54 PM
RE: Help with Sorting - by buran - Jan-28-2018, 05:14 PM
RE: Help with Sorting - by rsherry8 - Jan-28-2018, 05:38 PM
RE: Help with Sorting - by buran - Jan-28-2018, 07:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,170 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020