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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:57:20+00:00 2026-05-14T08:57:20+00:00

I have a 2D numpy array of shape (N,2) which is holding N points

  • 0

I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example:

array([[3, 2],
       [6, 2],
       [3, 6],
       [3, 4],
       [5, 3]])

I’d like to sort it such that my points are ordered by x-coordinate, and then by y in cases where the x coordinate is the same. So the array above should look like this:

array([[3, 2],
       [3, 4],
       [3, 6],
       [5, 3],
       [6, 2]])

If this was a normal Python list, I would simply define a comparator to do what I want, but as far as I can tell, numpy’s sort function doesn’t accept user-defined comparators. Any ideas?


EDIT: Thanks for the ideas! I set up a quick test case with 1000000 random integer points, and benchmarked the ones that I could run (sorry, can’t upgrade numpy at the moment).

Mine:   4.078 secs 
mtrw:   7.046 secs
unutbu: 0.453 secs
  • 1 1 Answer
  • 3 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-14T08:57:20+00:00Added an answer on May 14, 2026 at 8:57 am

    Using lexsort:

    import numpy as np    
    a = np.array([(3, 2), (6, 2), (3, 6), (3, 4), (5, 3)])
    
    ind = np.lexsort((a[:,1],a[:,0]))    
    
    a[ind]
    # array([[3, 2],
    #       [3, 4],
    #       [3, 6],
    #       [5, 3],
    #       [6, 2]])
    

    a.ravel() returns a view if a is C_CONTIGUOUS. If that is true,
    @ars’s method, slightly modifed by using ravel instead of flatten, yields a nice way to sort a in-place:

    a = np.array([(3, 2), (6, 2), (3, 6), (3, 4), (5, 3)])
    dt = [('col1', a.dtype),('col2', a.dtype)]
    assert a.flags['C_CONTIGUOUS']
    b = a.ravel().view(dt)
    b.sort(order=['col1','col2'])
    

    Since b is a view of a, sorting b sorts a as well:

    print(a)
    # [[3 2]
    #  [3 4]
    #  [3 6]
    #  [5 3]
    #  [6 2]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Numpy array that looks like >>> a array([[ 3. , 2.
I have a numpy.array of 640x480 images, each of which is 630 images long.
I have a numpy array of pixel data that I want to draw at
I have a 2d numpy array of bools, and I'd like to know how
I have a 3-dimensional numpy array. I'd like to display (in matplotlib) a nice
I have hundreds of thousands of NumPy boolean arrays that I would like to
I have a 2D numpy array that I want to plot in a colorbar.
I have a numpy array and I want to force every element that is
I have a numpy structured array with a dtype such as: A = numpy.empty(10,
Let's say I have a 2D Numpy array: >>> a = np.random.random((4,6)) and I

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.