I hope this can help:

COL = 0
DIM0 = 3
a[:, a[:, :, COL].argsort()][np.diag_indices(DIM0)]
Answer from YKang on Stack Overflow
🌐
Like Geeks
likegeeks.com › home › python › numpy › sorting numpy arrays: a comprehensive guide
Sorting NumPy Arrays: A Comprehensive Guide
In this code, we first created a 3D array. To sort this array along the last axis (i.e., axis=-1), we used the np.sort() function with axis=-1.
🌐
Stack Overflow
stackoverflow.com › questions › 74591019 › numpy-sort-3d-array-of-coordinates
python - Numpy Sort 3D Array of Coordinates - Stack Overflow
November 27, 2022 - arr = sorted(arr, key=lambda x: (x[0][1], x[0][0])) Then you can add arr = np.array(arr) to get a numpy array.
Top answer
1 of 2
2

First you need a single value which you can use to sort your cards. An easy one would be value*4 + suit:

sortval = deck[:,:,0]*4+deck[:,:,1]
sortval *= -1 # if you want largest first

Then you use np.argsort to find out which index belongs where and use it to sort your decks. It sorts along the last axis on default, which is what we want.

sortedIdx = np.argsort(sortval)

Now you can use it to sort your deck like this:

deck = deck[np.arange(len(deck))[:,np.newaxis],sortedIdx]

The np.arange... part makes sure that every second dimension index array from sortedIdx is paired with the right first dimension index.

The whole thing:

import numpy as np

deck = np.array([[[  6.,   2.],
                  [ 10.,   1.],
                  [  5.,   1.],
                  [  9.,   2.],
                  [  4.,   1.],
                  [  3.,   2.],
                  [ 11.,   2.]],

                 [[  6.,   2.],
                  [  2.,   2.],
                  [  2.,   3.],
                  [ 11.,   1.],
                  [ 11.,   3.],
                  [  5.,   3.],
                  [  4.,   4.]]])

sortval = deck[:,:,0]*4+deck[:,:,1]
sortval *= -1 # if you want largest first
sortedIdx = np.argsort(sortval)
deck = deck[np.arange(len(deck))[:,np.newaxis],sortedIdx]
print(deck)

Will print:

[[[ 11.   2.]
  [ 10.   1.]
  [  9.   2.]
  [  6.   2.]
  [  5.   1.]
  [  4.   1.]
  [  3.   2.]]

 [[ 11.   3.]
  [ 11.   1.]
  [  6.   2.]
  [  5.   3.]
  [  4.   4.]
  [  2.   3.]
  [  2.   2.]]]
2 of 2
0

Are you sorting the values only to see wich one has the highest value?? Because in this case why not use np.max()?:

deck=np.array([[[  6.,   2.],
                [ 10.,   1.],
                [  5.,   1.],
                [  9.,   2.],
                [  4.,   1.],
                [  3.,   2.],
                [ 11.,   2.]],
            [[  7.,   2.],
                [ 8.,   1.],
                [  1.,   1.],
                [  9.,   2.],
                [  4.,   1.],
                [  3.,   2.],
                [ 12.,   2.]]])

np.max(deck)
Out[4]: 12.0

np.max(deck[0])
Out[5]: 11.0
🌐
Quora
quora.com › How-can-I-sort-my-multidimensional-array
How to sort my multidimensional array - Quora
Answer (1 of 2): Look at how Python does it. A multidimensioned array (numpy.ndarray) in NumPy can be sorted along any one of the axes. For example, for a two-dimensioned array, first axis (axis=0) is the direction of the rows. Second axis (axis-1) is the direction of the columns. If axis=0, arr...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.sort.html
numpy.sort — NumPy v2.5 Manual
Array of the same type and shape as a. ... Method to sort an array in-place.
🌐
Studyopedia
studyopedia.com › home › sorting numpy arrays
Sorting Numpy Arrays - Studyopedia
March 18, 2026 - Sort a 2D array in NumPy (axis = 0) Sort a 3D array in NumPy(axis = 1) Sort a 3D array in NumPy(axis = 0) Sort a 3D array in NumPy(axis = 2) Let us start with the examples: # Sort a 1d integer array in NumPy # Ascending order sort (default) import numpy as np n = np.array([75, 89, 32, 54, 12, 8, 45, 78]) # sort print("Sorted array = ",np.sort(n)) Output ·
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_sort.asp
NumPy Sorting Arrays
The NumPy ndarray object has a function called sort(), that will sort a specified array.
Find elsewhere
🌐
Sharp Sight
sharpsight.ai › blog › numpy-sort
A quick guide to NumPy sort - Sharp Sight
February 6, 2024 - Imagine that you have a 1-dimensional NumPy array with five values that are in random order: You can use NumPy sort to sort those values in ascending order.
🌐
Codecademy
codecademy.com › article › sorting-and-unary-operations-in-num-py
Sorting and Unary Operations in NumPy | Codecademy
The fields correspond to the names of the columns in the array, and you can specify one or multiple field names as either a string or a list of strings. To sort a 1D array (similar to a Python list), we can use the numpy.sort() function.
🌐
Stack Overflow
stackoverflow.com › questions › 61449930 › is-there-another-way-for-sorting-3d-numpy-array-by-column-values
python - is there another way for sorting 3D numpy array by 'column' values? - Stack Overflow
I wrote a python code to sort a set of four 3x3 tables by the value of their first columns. Is there an easier way to do it, that is with less code, and maybe more efficient? Here is my code: import numpy as np np.random.seed(4) a = np.random.randint(10, size=(4, 3, 3)) ind = a[:,:,0].argsort() ind = np.stack(a.shape[2]*[ind], axis=1) b = np.take_along_axis(a.transpose(0, 2, 1), ind, axis=2).transpose(0, 2, 1) print(a) print("----------------") print(b)
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.08-sorting.html
Sorting Arrays | Python Data Science Handbook
The first element of this result gives the index of the smallest element, the second value gives the index of the second smallest, and so on. These indices can then be used (via fancy indexing) to construct the sorted array if desired: ... A useful feature of NumPy's sorting algorithms is the ability to sort along specific rows or columns of a multidimensional array using the axis argument.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.0 › reference › generated › numpy.ndarray.sort.html
numpy.ndarray.sort — NumPy v1.10 Manual
See sort for notes on the different sorting algorithms. ... >>> a = np.array([[1,4], [3,1]]) >>> a.sort(axis=1) >>> a array([[1, 4], [1, 3]]) >>> a.sort(axis=0) >>> a array([[1, 3], [1, 4]])
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 288688 › sorting-multidimensional-arrays
python - Sorting Multidimensional Arrays [SOLVED] | DaniWeb
June 8, 2010 - For lists in Python 2.4, key does exist, but only for list objects docs. To sort rows of a NumPy array by the first column (x) while keeping each row together, use argsort to build a row index and then index the array.
🌐
Wellsr
wellsr.com › python › sorting-numpy-arrays-in-python
How to Sort NumPy Arrays in Python (with Examples) - wellsr.com
April 29, 2022 - Array before sorting [42 17 30 ... 23 27 30 30 30 31 32 36 40 41 42 44 45 49] You can use the sort() method to sort NumPy arrays contaning text items (strings), as well....
🌐
CodingNomads
codingnomads.com › numpy-sort
NumPy Sort: How to Sort a NumPy Array
To sort arrays using NumPy sort, use the command np.sort() to return a sorted copy of an array without altering the original.