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 8813619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:54:05+00:00 2026-06-14T03:54:05+00:00

I have a n x 2 matrix of integers. The first column is a

  • 0

I have a n x 2 matrix of integers. The first column is a series 0,1,-1,2,-2, however these are in the order that they were compiled in from their constituent matrices. The second column is a list of indices from another list.

I would like to sort the matrix via this second column. This would be equivalent to selecting two columns of data in Excel, and sorting via Column B (where the data is in columns A and B). Keep in mind, the adjacent data in the first column of each row should be kept with its respective second column counterpart. I have looked at solutions using the following:

data[np.argsort(data[:, 0])]

But this does not seem to work. The matrix in question looks like this:

matrix([[1, 1],
        [1, 3],
        [1, 7],
        ..., 
        [2, 1021],
        [2, 1040],
        [2, 1052]])
  • 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-06-14T03:54:07+00:00Added an answer on June 14, 2026 at 3:54 am

    You could use np.lexsort:

    numpy.lexsort(keys, axis=-1)

    Perform an indirect sort using a sequence of keys.

    Given multiple sorting keys, which can be interpreted as columns in a
    spreadsheet, lexsort returns an array of integer indices that
    describes the sort order by multiple columns.


    In [13]: data = np.matrix(np.arange(10)[::-1].reshape(-1,2))
    
    In [14]: data
    Out[14]: 
    matrix([[9, 8],
            [7, 6],
            [5, 4],
            [3, 2],
            [1, 0]])
    
    In [15]: temp = data.view(np.ndarray)
    
    In [16]: np.lexsort((temp[:, 1], ))
    Out[16]: array([4, 3, 2, 1, 0])
    
    In [17]: temp[np.lexsort((temp[:, 1], ))]
    Out[17]: 
    array([[1, 0],
           [3, 2],
           [5, 4],
           [7, 6],
           [9, 8]])
    

    Note if you pass more than one key to np.lexsort, the last key is the primary key. The next to last key is the second key, and so on.


    Using np.lexsort as I show above requires the use of a temporary array because np.lexsort does not work on numpy matrices. Since
    temp = data.view(np.ndarray) creates a view, rather than a copy of data, it does not require much extra memory. However,

    temp[np.lexsort((temp[:, 1], ))]
    

    is a new array, which does require more memory.

    There is also a way to sort by columns in-place. The idea is to view the array as a structured array with two columns. Unlike plain ndarrays, structured arrays have a sort method which allows you to specify columns as keys:

    In [65]: data.dtype
    Out[65]: dtype('int32')
    
    In [66]: temp2 = data.ravel().view('int32, int32')
    
    In [67]: temp2.sort(order = ['f1', 'f0'])
    

    Notice that since temp2 is a view of data, it does not require allocating new memory and copying the array. Also, sorting temp2 modifies data at the same time:

    In [69]: data
    Out[69]: 
    matrix([[1, 0],
            [3, 2],
            [5, 4],
            [7, 6],
            [9, 8]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column matrix of integers like [1;4;3;2;3] . How to count the
I have NxM matrix with integer elements, greater or equal than 0. From any
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix A with size (nr,nc), a vector of column indices B
Let's say I have a NxN matrix full of random integers in the range
I have a 2D-array that I want to sort based on the second column.
Lets say we have the 10 integers from 1 to 10. We also have
I have a class that stores a list of lists in a matrix style,
I am trying to create a scatterplot matrix from my dataset so that in

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.