Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 791099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:46:56+00:00 2026-05-14T21:46:56+00:00

How do I sort a NumPy array by its nth column? For example, given:

  • 0

How do I sort a NumPy array by its nth column?

For example, given:

a = array([[9, 2, 3],
           [4, 5, 6],
           [7, 0, 5]])

I want to sort the rows of a by the second column to obtain:

array([[7, 0, 5],
       [9, 2, 3],
       [4, 5, 6]])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:46:56+00:00Added an answer on May 14, 2026 at 9:46 pm

    @steve‘s answer is actually the most elegant way of doing it.

    For the “correct” way see the order keyword argument of numpy.ndarray.sort

    However, you’ll need to view your array as an array with fields (a structured array).

    The “correct” way is quite ugly if you didn’t initially define your array with fields…

    As a quick example, to sort it and return a copy:

    In [1]: import numpy as np
    
    In [2]: a = np.array([[1,2,3],[4,5,6],[0,0,1]])
    
    In [3]: np.sort(a.view('i8,i8,i8'), order=['f1'], axis=0).view(np.int)
    Out[3]: 
    array([[0, 0, 1],
           [1, 2, 3],
           [4, 5, 6]])
    

    To sort it in-place:

    In [6]: a.view('i8,i8,i8').sort(order=['f1'], axis=0) #<-- returns None
    
    In [7]: a
    Out[7]: 
    array([[0, 0, 1],
           [1, 2, 3],
           [4, 5, 6]])
    

    @Steve’s really is the most elegant way to do it, as far as I know…

    The only advantage to this method is that the “order” argument is a list of the fields to order the search by. For example, you can sort by the second column, then the third column, then the first column by supplying order=[‘f1′,’f2′,’f0’].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort an numpy array according to the sum. Something like import
I have this following numpy matrix that I want to sort in ascending order
I'm trying to sort the rows of one array by the values of another.
I sort the rows on date. If I want to select every row that
I need to sort a special numpy array, in which blocks of size 19
I am trying to sort a numpy array using this very simple code: print
I want to replace the max values of each column of a 2d numpy
I created a python .py with some sort methods. I'm using numpy so that
...@Sort bit) AS SELECT .............. and I want to Order only if Sort =
Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.