0

So I've made an array with the following values in it:

array = [[621, 685, '3'], [933, 618, '4'], [510, 392, '2'], [792, 280, '1']]

and these values are [x, y, number]. What I'm trying to do it to sort this array by the number value in each index, so that the array will look like this:

array = [[792, 280, '1'], [510, 392, '2'], [621, 685, '3'], [933, 618, '4']]

But how do I do this?

I've begun a function to make this, but I'm stuck

def sortNumbers(Cubes):

    print("array = ", Cubes)
    Cubes = np.split(Cubes, [0,2])
    for x in Cubes:
        print(x)
4
  • 7
    sorted(array, key=lambda x: int(x[2]))? Commented Nov 25, 2020 at 23:37
  • @ggorlen Thanks! now I get the right when I print print("array", sorted(Cubes, key=lambda x: int(x[2]))) didn't know I could do this! Commented Nov 25, 2020 at 23:40
  • @ggorlen posted your solution in the comments Commented Nov 25, 2020 at 23:51
  • Keep in mind that the ordering between strings that are numerical and numbers is not the same. Are you trying to treat them as strings or numbers? Commented Nov 26, 2020 at 1:36

4 Answers 4

1

Hope this helped you.

array =  [[621, 685, '3'], [933, 618, '4'], [510, 392, '2'], [792, 280, '1']]
sorted_array = sorted(array, key=lambda kv: kv[2])
print(sorted_array)

[Result]

[[792, 280, '1'], [510, 392, '2'], [621, 685, '3'], [933, 618, '4']]

Sign up to request clarification or add additional context in comments.

Comments

1

I did some research and found something, and the code is

# of sublist Inplace way to sort using sort() 
def Sort(array): 

    # reverse = None (Sorts in Ascending order) 
    # key is set to sort using second element of 
    # sublist lambda has been used 
    array.sort(key = lambda x: x[2]) 
    return array 

# Driver Code 
array =  [[621, 685, '3'], [933, 618, '4'], [510, 392, '2'], [792, 280, '1']]
print(Sort(array))

here's a link if you wanna check more about it: https://www.geeksforgeeks.org/python-sort-list-according-second-element-sublist/

Comments

0

Credits to @ggorlen for awnsering this in the commends:

Solution

def sortNumbers(Cubes):
    Cubes = sorted(Cubes, key=lambda x: int(x[2]))
    print("Sorted array = ", sorted(Cubes, key=lambda x: int(x[2])))

    return Cubes 

Comments

0

Python's operator module has a function itemgetter that does what you want. It creates a function that returns the Nth item (or items!) in the thing you pass to it. For example:

>>> from operator import itemgetter
>>> third = itemgetter(2)
>>> third(['a', 'b', 'c'])
'c'

That makes a wonderful key function for list.sort or sorted:

array.sort(key=itemgetter(2))  # In-place sorting
new_array = sorted(array, key=itemgetter(2))  # Create a new list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.